Posts

Showing posts with the label TreeMap

Differences between Map implementations - HashMap, TreeMap, LinkedHashMap, WeakHashMap, IdentityHashMap, EnumMap

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. HashMap, TreeMap, LinkedHashMap, WeakHAshMap, IdentityHashMap, EnumMap     HashMap is implemented as a hash table, and there is no ordering on keys or values.  TreeMap is implemented based on red-black tree structure, and it is ordered by the key. Key object should implement Comparable interface inorder to compare with other keys and maintain sorted order.  TreeMap is not synchronized. We can make it synchronized by using Collections.synchronizedMap method. Ex: Simple example which outputs the natural ordering of key elements.  Map treeMap = new TreeMap(); treeMap.put("f",10); treeMap.put("d", 4); treeMap.put("a", 1); treeMap.put("b", 2); output :  a=1, b=2, d=4, f=10