AdSense

AdSense3

Tuesday 8 September 2015

Java LinkedHashMap class

  • A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It is same as HashMap instead maintains insertion order.

Hierarchy of LinkedHashMap class:

LinkedHashMap class hierarchy

Example of LinkedHashMap class:

  1. import java.util.*;  
  2. class TestCollection14{  
  3.  public static void main(String args[]){  
  4.    
  5.   LinkedHashMap<Integer,String> hm=new LinkedHashMap<Integer,String>();  
  6.   
  7.   hm.put(100,"Amit");  
  8.   hm.put(101,"Vijay");  
  9.   hm.put(102,"Rahul");  
  10.   
  11. for(Map.Entry m:hm.entrySet()){  
  12.    System.out.println(m.getKey()+" "+m.getValue());  
  13.   }  
  14.  }  
  15. }  
Output:100 Amit
       101 Vijay
       103 Rahul

No comments:

Post a Comment