Tuesday, 24 November 2015

Know LinkedHashMap in Depth

LinkedHashMap is a map which extends HashMap class and implements Map interface. But the only different between LinkedHashMap and HashMap is LinkedHashMap maintains the insertion order whereas HashMap doesn't. LinkedHashMap uses double LinkedList internally to maintain the order.

Example :

class Employee{
    String id;
    String name;
    public Employee(String id, String name) {
        this.id = id;
        this.name = name;
    @Override    public String toString() {
        return "["+id+","+name+"]";

import java.util.HashMap;
import java.util.LinkedHashMap;

public class LinkedHashMapDemo1 {
    public static void main(String[] args) {
        LinkedHashMap<Employee, Integer> linkedMap = new LinkedHashMap<>();
        HashMap<Employee, Integer> hashMap = new HashMap<>();

        System.out.println("LinkedHashMap : ");
        linkedMap.put(new Employee("1001","Alpha"), 2000);
        linkedMap.put(new Employee("1002","Beta"), 2000);
        linkedMap.put(new Employee("1003","Gama"), 2000);

        System.out.println("HashMap : ");
        hashMap.put(new Employee("1001","Alpha"), 2000);
        hashMap.put(new Employee("1002","Beta"), 2000);
        hashMap.put(new Employee("1003","Gama"), 2000);
LinkedHashMap : 
{[1001,Alpha]=2000, [1002,Beta]=2000, [1003,Gama]=2000}
HashMap : 
{[1001,Alpha]=2000, [1003,Gama]=2000, [1002,Beta]=2000}

Here LinekedHashMap keeps the insertion order but HashMap doesn't.

Now the Secret of LinkedHashMap :

LinkedHashMap provided a special constructor to maintain access order instead insertion order. public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) Access Order is like maintaining the order of the entries accessed. Access Order is affected by get(key), put(key,val) and putAll(Map). Because of this nature, LinkedHashMap is used in LRU(Least Recent Used) Caching.
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapTest {
    public static void main(String[] args) {
        LinkedHashMap<String, Integer> linkedMap = new LinkedHashMap<>();
        System.out.println("Default LinkedHashMap : ");
        linkedMap.put("Gama", 2000);
        linkedMap.put("Alpha", 2000);
        linkedMap.put("Beta", 2000);
        // Once Added after that no change in order
        linkedMap.put("Alpha", 3000);
        linkedMap.put("Beta", 3000);
        linkedMap.put("Gama", 3000);

        // LinkedHashMap with accessOrder        
        System.out.println("LinkedHashMap with accessOrder  = TRUE: ");
        LinkedHashMap<String, Integer> linkedAccessedOrderMap = 
                        new LinkedHashMap<>(10, 0.5f, true);
        linkedAccessedOrderMap.put("Gama", 2000);
        linkedAccessedOrderMap.put("Alpha", 2000);
        linkedAccessedOrderMap.put("Beta", 2000);
        linkedAccessedOrderMap.put("Alpha", 3000);
Default LinkedHashMap : 
{Gama=3000, Alpha=3000, Beta=3000}
LinkedHashMap with accessOrder  = TRUE: 
{Beta=2000, Alpha=3000, Gama=2000}

No comments:

Post a Comment