Java Collections

Difference between TreeMap vs HashMap

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<p>Both <i><b>TreeMap <&sol;b><&sol;i>&amp&semi; <i><b>HashMap <&sol;b><&sol;i>are two different implementations of the <i><b>Map <&sol;b><&sol;i>interface&period; Even though this post is titled &OpenCurlyDoubleQuote;<i><b>TreeMap <&sol;b><&sol;i>vs <i><b>HashMap<&sol;b><&sol;i>” I would like to say how they are connected and how much similar they are&period;<&sol;p>&NewLine;<p>Both <i><b>TreeMap <&sol;b><&sol;i>&amp&semi; <i><b>HashMap <&sol;b><&sol;i>are not synchronized&period; To make it synchronized we have to explicitly call Collections&period;synchronizedMap&lpar; mapName&rpar;&period; Both supports &OpenCurlyDoubleQuote;fail-fast” iterators&period; Both of them doesn’t support duplicate keys&period;<&sol;p>&NewLine;<p><i><b>HashMap<&sol;b><&sol;i><&sol;p>&NewLine;<p><i><b>1&period; HashMap <&sol;b><&sol;i>allows null as both keys and values&period;<br &sol;>&NewLine;<i><b>2&period; HashMap<&sol;b><&sol;i> is useful when we need to access the map without considering how they are added to the map &lpar;means&comma; unordered lookup of values using their keys&rpar;&period;<br &sol;>&NewLine;<i><b>3&period; HashMap<&sol;b><&sol;i> is not synchronized while it is being looked up&period;<br &sol;>&NewLine;<i><b>4&period; HashMap<&sol;b><&sol;i> doesn’t allow duplicated entries&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p>The performance of <i><b>HashMap <&sol;b><&sol;i>is based on two optional parameters which we can specify during the creation of the <i><b>HashMap<&sol;b><&sol;i>&period; Initial capacity &amp&semi; load factor&period; Initial capacity is the bucket size assigned to a <i><b>HashMap<&sol;b><&sol;i> during its creation&period; Load factor decides when the <i><b>HashMap <&sol;b><&sol;i>needs to be expanded&period; If the load factor is 0&period;75&comma; the size will be increased when the current size of the map crosses 75&percnt; of its capacity&period;<&sol;p>&NewLine;<p><i><b>TreeMap<&sol;b><&sol;i><&sol;p>&NewLine;<p>The basic difference between <i><b>HashMap &amp&semi; TreeMap<&sol;b><&sol;i> is that&comma;<br &sol;>&NewLine;1&period; in a <i><b>TreeMap <&sol;b><&sol;i>the elements are stored in a tree&period;<br &sol;>&NewLine;2&period;<i><b>TreeMap <&sol;b><&sol;i>allows us to retrieve the elements in some sorted order defined by the user&period; So we can say that <i><b>TreeMap <&sol;b><&sol;i>is slower than <i><b>HashMap<&sol;b><&sol;i>&period; This is the only implementation based on <i><b>a SortedMap <&sol;b><&sol;i>interface&period;<&sol;p>&NewLine;<p><i><b>TreeMap <&sol;b><&sol;i>allows us to specify an optional <i><b>Comparator <&sol;b><&sol;i>object during its creation&period; The keys should be compatible with the comparator specified&period; This comparator decides the order by which the keys need to be sorted&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public interface Comparator &NewLine;&lbrace; &NewLine; public int compare &lpar;Object object1&comma; Object object2&rpar;&semi; &NewLine; public boolean equals &lpar;Object object&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>If we are not specifying the comparator&comma; <i><b>TreeMap <&sol;b><&sol;i>will try to sort the keys in the natural order&period; Means will consider them as instances of <i><b>Comparable <&sol;b><&sol;i>interface&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public interface Comparable &NewLine;&lbrace; &NewLine; public int compareTo &lpar;Object objectToCompare&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>Classes like <i><b>Integer&comma; String&comma; Double<&sol;b><&sol;i> etc implement <i><b>the Comparable <&sol;b><&sol;i>interface&period; So if we are to use an object of a custom class as the key&comma; ensure that it’ s class implements the <i><b>Comparable <&sol;b><&sol;i>interface&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public class MyCustomKey implements Comparable &NewLine;&lbrace; &NewLine; private int value&semi; &NewLine; public MyCustomKey&lpar;int value&rpar; &NewLine; &lbrace; &NewLine; this&period;value &equals; value&semi; &NewLine; &rcub; &NewLine; &NewLine; public int compareTo &lpar;MyCustomKey key&rpar; &NewLine; &lbrace; &NewLine; int comparison &equals; 0&semi; &NewLine; &NewLine; &sol;&sol; Note&colon; &NewLine; &sol;&sol; Return -1 if this&period;value &lt&semi; key&period;value &NewLine; &sol;&sol; Return 0 if this&period;value &equals; key&period;value &NewLine; &sol;&sol; Return 1 if this&period;value &gt&semi; key&period;value &NewLine; &NewLine; return &lpar;comparison&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>A common mistake that everyone does is not to override the <i><b>hashcode&lpar;&rpar;<&sol;b><&sol;i>&period; If we are failing to do so&comma; <i><b>map&period;get&lpar;new MyCustomKey&lpar;&lt&semi;value&gt&semi;&rpar;&rpar;<&sol;b><&sol;i>&semi; may not give you what you were expecting&period; So it is always advisable to override the <i><b>hashCode&lpar;&rpar;<&sol;b><&sol;i> if objects of that class are being used as a key&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public class MyCustomKey implements Comparable &NewLine;&lbrace; &NewLine; private int value&semi; &NewLine; public MyCustomKey&lpar;int value&rpar; &NewLine; &lbrace;&rcub; &NewLine; public int compareTo &lpar;MyCustomKey key&rpar; &NewLine; &lbrace;&rcub; &NewLine; &NewLine; public int hashCode&lpar;&rpar; &NewLine; &lbrace; &NewLine; &sol;&sol; Note&colon; &NewLine; &sol;&sol; If two objects are equal then their corresponding hashCode &lpar;&rpar; &NewLine; &sol;&sol; should return the same value too&period; &NewLine; return &lpar;this&period;value &ast; 199&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;"><b>Java Collections Tutorial<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;arraylist-class-in-java-collection&sol;">ArrayList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedlist-class-in-collection&sol;">LinkedList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;listiterator-interface-in-collection&sol;">ListIterator interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;">HashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashset-class-in-collection&sol;">LinkedHashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;">TreeSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashset-and-treeset&sol;">Difference between TreeSet &amp&semi; HashSet<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;map-interface-in-collection&sol;">Map interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashmap-class-in-collection-framework&sol;">HashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-does-java-hashmap-work-internally&sol;">How does work HashMap&quest;<&sol;a> <&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashmap-class-in-collection&sol;">LinkedHashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treemap-class-in-collection-framework&sol;">TreeMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashtable-class-in-collection-framework&sol;">Hashtable class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashmap-and&sol;">Difference between HashMap and HashTable in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;sorting-in-collection-framework&sol;">Sorting<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-comparable-and-comparator&sol;">Comparable interface<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treemap-class-in-collection-framework&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashtable-class-in-collection-framework&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treemap-class-in-collection-framework&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashtable-class-in-collection-framework&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '410'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago