Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 12 December 2016

What is the difference between HashMap and TreeMap In Java ?


Here we list all differences between HashMap and TreeMap In Java.

Know Also :  Map / HashMap / TreeMap / AbstractMap / NavigableMap

HashMap
TreeMap
It doesn’t maintain insertion order of elements.
TreeMap by default maintain natural order of elements i.e ascending order.
It allows one null key and multiple null values.
It doesn’t allow null as a key or as a value.
Its performance is fast because it doesn’t maintain any order.
Its performance is slow in comparison to HashMap because it maintains element order.
It internally uses Hashing.
It internally uses Red Black Tree.
HashMap is not rich in functionality as comparison with TreeMap.
TreeMap is rich in functionality because it has some methods like pollFirstEntry(), pollLastEntry(), tailMap(), firstKey(), lastKey() etc.
HashMap uses equal() method for comparison.
TreeMap uses campareTo() method for comparison.

Some similarities between HashMap and TreeMap In Java are as follows :

HashMap
TreeMap
It is not synchronized.
It is not synchronized.
It is not Thread-safe.
It is not Thread-safe.
It is a fail-fast iterator.
It is a fail-fast iterator.
It extends AbstractMap class.
It extends AbstractMap class.
Hashmap can be synchronized by using :
HashMap<String, String> map = new HashMap<>();
Collections.synchronizedMap(map);
TreeMap can be synchronized by using :
TreeMap<String, Integer> treemap = new TreeMap<>();
Collections.synchronizedSortedMap(treemap);


     
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment