Menu
Topics Index
...
`


Collections Framework > Map Classes >
Siva Nookala - 20 Feb 2017
The Class LinkedHashMap is an extension of Java HashMap Implementation with specific feature of retaining the insertion order in the order, in which they were inserted. Also if one inserts the key again into the LinkedHashMap the original orders is retained.

This allows insertion-order iteration over the Java Map Interfaces - HashMap, TreeMap, LinkedHashMap. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted. You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed.

LinkedHashMap Constructors :
Constructor Description
LinkedHashMap() Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).
LinkedHashMap(int initialCapacity) Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and a default load factor (0.75).
LinkedHashMap(int initialCapacity, float loadFactor) Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor.
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
LinkedHashMap(Map<? extends K,? extends V> m) Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.

In addition to those methods defined by Map and HashMap, the LinkedHashMap includes these methods,
Method Description
void clear() Removes all of the mappings from this map.
boolean containsKey(Object key) Returns true if this map maps one or more keys to the specified value.
Object get(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
protected boolean removeEldestEntry(Map.Entry eldest) Returns true if this map should remove its eldest entry.

Linked Hash Map
import java.util.*;

class LinkedHashMapTest
{
    public static void main(String arg[])
    {
        LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
        lhm.put("Ramesh", "Intermediate");
        lhm.put("Shiva", "B-Tech");
        lhm.put("Santosh", "B-Com");
        lhm.put("Asha", "Msc");
        lhm.put("Raghu", "M-Tech");
        
        Set set = lhm.entrySet();
        Iterator i = set.iterator();
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            System.out.println(me.getKey() + " : " + me.getValue());
        }
                
        System.out.println("The Key Contains : " + lhm.containsKey("Shiva"));
        System.out.println("The value to the corresponding to key : " + lhm.get("Asha"));    
    }
}
OUTPUT

Ramesh : Intermediate
Shiva : B-Tech
Santosh : B-Com
Asha : Msc
Raghu : M-Tech
The Key Contains : true
The value to the corresponding to key : Msc

DESCRIPTION

In this program, LinkedHashMap object is created of both key and value types are String. Five records are inserted into the LinkedHashMap and then iterating the set LinkedHashMap using Map.Entry and printing the key and values as inserted into it.

THINGS TO TRY
  • Take LinkedHashMap type of key String and value of type Integer for adding student details given below and print the LinkedHashMap.
    [Raja=50, Sareesh=45, Hemanth=40, Srinath=50]
  • If you provide key which does not exists in LinkedHashMap at containsKey null will be returned. Try for the below code.
    lhm.containsKey("Raja");
  • You can apply Map and Hash Map methods to LinkedHashMap.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App