LinkedHashMap Class

java.util.LinkedHashMap;
public class LinkedHashMap<K,V> extends HashMap<K,V>
implements Map<K,V>
The LinkedHashMap class implements the Map interface extends HashMap. LinkedHashMap maintains insertion-order a linked. When iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.
LinkedHashMap class features:
  • A TreeMap cannot contain duplicate key.
  • It cantain one null key and contain multiple null value .
  • It maintains Key insertion order/Access order of entries .
  • hashCode() and equals() methods is used for its elements .
  • Data structure is based on Hash table and doubly-linked list.
Constructor of LinkedHashMap class
The LinkedHashMap class supports five constructors
  1. public LinkedHashMap( )
                This counstructor is used to create an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).
  2. public LinkedHashMap(Map m)
                This counstructor is used to create an insertion-ordered LinkedHashMap instance with the same mappings as the specified map. The LinkedHashMap instance is created with a default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified map.
  3. public LinkedHashMap(int capacity)
                This counstructor is used to create an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and a default load factor (0.75).
  4. public LinkedHashMap(int capacity, float load)
                This counstructor is used to create an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor.
  5. LinkedHashMap(int capacity, float fillRatio, boolean Order)
                This constructor allows us to specify whether the elements will be stored in the linked list by insertion order, or by order of last access. If Order is true, then access order is used. If Order is false, then insertion order is used.
Method of LinkedHashMap class
LinkedHashMap adds only one method to those defined by HashMap.
  1. protected boolean removeEldestEntry(Map.Entry e)
    This method is called by put( ) and putAll( ). The oldest entry is passed in e. By default, this method returns false and does nothing. However, if we override this method, then we can have the LinkedHashMap remove the oldest entry in the map. To do this, have our override return true. To keep the oldest entry, return false.
Example of LinkedHashMap class
import java.util.LinkedHashMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class LinkedHashMapDemo { public static void main(String args[]) { LinkedHashMap lhm = new LinkedHashMap(); Set set; Map.Entry m; double bal; lhm.put("Sunil",134.34); lhm.put("Anand", 1023.22); lhm.put("Sanjeev",15678.00); lhm.put("Sujeet", 5647.22); // Show all set of hash Map. set = lhm.entrySet(); // get iterator Iterator it=set.iterator(); while(it.hasNext()) { m = (Map.Entry) it.next(); System.out.println(m.getKey() + ": " + m.getValue()); } System.out.println(); // Add 1350.67 into Sunil value bal = ((Double)lhm.get("Sunil")).doubleValue(); lhm.put("Sunil", new Double(bal+1350.67)); System.out.println("Sunil new Value: " + lhm.get("Sunil")); } }
 
Output


Sunil: 134.34
Anand: 1023.22
Sanjeev: 15678.0
Sujeet: 5647.22

Sunil new Value: 1485.01

Download this example
 



 

Latest Tutorial


Most Visited Tutorial