Difference between HashMap and LinkedHashMap?

Difference between HashMap and LinkedHashMap?

Difference between HashMap and LinkedHashMap?

Difference between HashMap and LinkedHashMap?
  • It is the child class of HashMap.
  • The LinkedHashMap class is very similar to HashMap in most aspects. However, the linked hash map is based on both hash table and the linked list to enhance the functionality of hash map. It maintains a doubly-linked list running through all its entries in addition to an underlying array of default size 16.
  • This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation).
  • Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map.
  • It is exactly same as HashMap (Including methods and constructors) except the following differences
No HashMap LinkedHashMap
1 The underlying data Structure is Hashtable. Underlying data-structure is a combination of LinkedList+ Hashtable (Hybrid data Structure)
2 Insertion order is not preserved and it is based on HashCode of keys. Insertion order is preserved.
3 Introduced in 1.2 version Introduced in 1.4 version
package com.java4us;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

class Test {
    public static void main(String[] args) {
        LinkedHashMap map = new LinkedHashMap();
        map.put(101, "Welcome");
        map.put(104, "To");
        map.put(105, "Java4us");
        map.put(102, "Dot");
        map.put(103, "Com");
        System.out.println(map); // {101=Welcome, 102=To, 103=Java4us, 104=Dot, 105=Com}
        Set s = map.keySet();
        System.out.println(s); // [101, 102, 103, 104, 105]
        Collection c = map.values();
        System.out.println(c); // [Welcome, To, Java4us, Dot, Com]
        Set set = map.entrySet();
        System.out.println(set); // [101=Welcome, 102=To, 103=Java4us, 104=Dot, 105=Com]
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry m1 = (Map.Entry) iterator.next();
            System.out.println(m1.getKey() + ".." + m1.getValue());
            if (m1.getValue().equals("Dot")) {
                m1.setValue(".");
            }
        }
        System.out.println(map); // {101=Welcome, 102=To, 103=Java4us, 104=., 105=Com}
    }
}
  • In the above HashMap program if we replace HashMap with LinkedHashMap then the output is based on insertion order.
  • LinkedHashSet and LinkedHashMap are commonly used for developing the cache based application.

Leave a Reply

Your email address will not be published. Required fields are marked *

*