Showing posts with label LinkedHashMap. Show all posts
Showing posts with label LinkedHashMap. Show all posts

Difference between Hashtable, HashMap, TreeMap and LinkedHashMap

 
Map is one of the most important data structures and when we talk about Map all these 4 implementation classes (Hashtable, HashMap, TreeMap and LinkedHashMap) are most used. Hashtable, HashMap and LinkedHashMap are directly implements Map interface, but TreeMap implements Map interface through implementing SortedMap. Below are the few differences between these classes
Difference between Hashtable, HashMap, TreeMap and LinkedHashMap
  • HashMap implemented as same as Hashtable except it is unsynchronized and permits null. As common both HashMap and Hashtable won't have any ordering on keys or values.
  • TreeMap implemented based on red-black tree structure and it follows natural ordering on key. 
  • LinkedHashMap is a subclass of HashMap and inherits all properties of HashMap class and additionally its ordering will based on insertion order.
NOTE:
  • If we use any Map implementation classes and if the key is user-defined class Object then we need to override hashcode() and equals() methods. 
  • Next when we use TreeMap, by default they are sorted by keys. If we use user-defined class Object for key, then to compare with each other we need to implement Comparable interface.

Next lets see simple examples for each classes. 

Hashtable:


import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

public class MapTest {

 public static void main(String[] args) {

  Map<String, String> map = new Hashtable<String, String>();
  map.put("1", "one");
  map.put("2", "two");
  map.put("3", "three");
  map.put("4", "four");
  map.put("5", "five");

  Iterator<?> itr = map.entrySet().iterator();
  while (itr.hasNext()) {
   Map.Entry myMap = (Map.Entry) itr.next();
   System.out.println(myMap.getKey() + " : " + myMap.getValue());
  }
 }
}

OUTPUT:


5 : five
4 : four
3 : three
2 : two
1 : one


HashMap:


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapTest {

 public static void main(String[] args) {

  Map<String, String> map = new HashMap<String, String>();
  map.put("1", "one");
  map.put("2", "two");
  map.put("3", "three");
  map.put("4", "four");
  map.put("5", "five");

  Iterator<?> itr = map.entrySet().iterator();
  while (itr.hasNext()) {
   Map.Entry myMap = (Map.Entry) itr.next();
   System.out.println(myMap.getKey() + " : " + myMap.getValue());
  }
 }
}

OUPTUT:


3 : three
2 : two
1 : one
5 : five
4 : four


TreeMap:


import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class MapTest {

 public static void main(String[] args) {

  Map<String, String> map = new TreeMap<String, String>();
  map.put("2", "two");
  map.put("3", "three");
  map.put("1", "one");
  map.put("4", "four");
  map.put("5", "five");

  Iterator<?> itr = map.entrySet().iterator();
  while (itr.hasNext()) {
   Map.Entry myMap = (Map.Entry) itr.next();
   System.out.println(myMap.getKey() + " : " + myMap.getValue());
  }
 }
}

OUTPUT:


1 : one
2 : two
3 : three
4 : four
5 : five


LinkedHashMap:


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

public class MapTest {

 public static void main(String[] args) {

  Map<String, String> map = new LinkedHashMap<String, String>();
  map.put("2", "two");
  map.put("3", "three");
  map.put("1", "one");
  map.put("4", "four");
  map.put("5", "five");

  Iterator<?> itr = map.entrySet().iterator();
  while (itr.hasNext()) {
   Map.Entry myMap = (Map.Entry) itr.next();
   System.out.println(myMap.getKey() + " : " + myMap.getValue());
  }
 }
}

OUTPUT:


2 : two
3 : three
1 : one
4 : four
5 : five

LinkedHashMap

LinkedHashMap is under Java Collection API and it implements Map interface. LinkedHashMap also works as same as HashMap with only 1 difference. Lets see the properties of LinkedHashMap class in Java

  • It works based on key and value as same as HashMap.
  • It implements Map interface and extends HashMap class, so that it acquires all properties of HashMap class. 
  • It can hold 1 null key and multiple null values. 
  • Only difference between HashMap and LinkedHashMap is interstion order. LinkedHashMap maintains the insertion order where as in HashMap its not. 

Now lets see simple example for LinkedHashMap and how to use it and also lets test the insertion order in both collection classes.


import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class LinkedHashMapTest {

 public static void main(String[] args) {
  
  Map<String, String> hm = new HashMap<String, String>();
  Map<String, String> lhm = new LinkedHashMap<String, String>();
  
  hm.put("1", "one"); 
  hm.put("2", "two");
  hm.put("3", "three");
  hm.put("4", "four");
  
  lhm.put("1", "one"); 
  lhm.put("2", "two");
  lhm.put("3", "three");
  lhm.put("4", "four");
  
  // Printing HashMap values.
  System.out.println("HasnMap Key and Value :::::::::::::::");
  Set<Entry<String, String>> set = hm.entrySet();
  Iterator<Entry<String, String>> itr = set.iterator();
  while(itr.hasNext()){
   Map.Entry<String, String> map = itr.next();
   System.out.println(map.getKey() +" : "+map.getValue());
  }
  
  // Printing LinkedHashMap values.
  System.out.println("HasnMap Key and Value :::::::::::::::");
  Set<Entry<String, String>> set1 = lhm.entrySet();
  Iterator<Entry<String, String>> itr1 = set1.iterator();
  while(itr1.hasNext()){
   Map.Entry<String, String> map1 = itr1.next();
   System.out.println(map1.getKey() +" : "+map1.getValue());
  }
 }
}


OUTPUT:


HasnMap Key and Value :::::::::::::::
3 : three
2 : two
1 : one
4 : four

HasnMap Key and Value :::::::::::::::
1 : one
2 : two
3 : three
4 : four


In above program we have added 4 values in HashMap and LinkedHashMap. Next when we print those key and value we can see the difference between both like HashMap insertion order not maintained while printing. Where as in LinkedHashMap its same as insertion order.