Tuesday, 20 September 2016

TreeMap vs LinkedHashMap

Here I am going to explain difference between HashMap vs TreeMap  vs LinkedHashMap

HashMap 

     HashMap accepts key-value pair,It allows null value for both key and Value and HashMap is Unsynchronized.

  HashMap<String,String> hashmap=new HashMap<String,String>();

  hashmap.put("1","Dhina");
  hashmap.put("2",null);


TreeMap  

    TreeMap accepts key-value pair,It didn't allows null value for key and  allows null value for Value and TreeMap is Unique.It is same as HashMap instead maintains ascending order.

  TreeMap <String,String> treemap=new TreeMap <String,String>();

  treemap.put("1","Dhina");
  treemap.put("2",null);




LinkedHashMap

    LinkedHashMap accepts key-value pair,It allows one null  key and  allows null value for Value and LinkedHashMap is Unique.It is same as HashMap instead maintains insertion order.

  LinkedHashMap <String,String> linkedhash=new LinkedHashMap<String,String>();

  linkedhash.put("1","Dhina");
  linkedhash.put("null",Karan);