Java Collections

Difference between HashSet and TreeSet in Java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">Several difference between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet <&sol;b><&sol;i><&sol;a>are similar to what we discussed as difference between <i><b>TreeMap <&sol;b><&sol;i>and <i><b>HashMap<&sol;b><&sol;i>&period; Anyway Set and Map are two completely different interface so we will revisit those differences here&period; Probably most important difference between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet <&sol;b><&sol;i><&sol;a>is the performance&period; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>is faster than <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet <&sol;b><&sol;i><&sol;a>which means if you need performance use <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>but <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>doesn&&num;8217&semi;t provide any kind of ordering so if you need ordering then you need to switch to <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet<&sol;b><&sol;i><&sol;a> which provides sorting of keys&period; Sorting can be natural order defined by Comparable interface or any particular order defined by Comparator interface in Java&period; Apart from differences between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet <&sol;b><&sol;i><&sol;a>there are some common things between them&period; let&&num;8217&semi;s see what is common between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet <&sol;b><&sol;i><&sol;a>in Java&period;<br &sol;>&NewLine;1&rpar; First major difference between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>is performance&period; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>is faster than <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet<&sol;i><&sol;b><&sol;a> and should be preferred choice if sorting of element is not required&period;<&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p>2&rpar; Second difference between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <i><b>TreeSet <&sol;b><&sol;i>is that <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><b><i>HashSet <&sol;i><&sol;b><&sol;a>allows null object but <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>doesn&&num;8217&semi;t allow null Object and throw <i><b>NullPointerException<&sol;b><&sol;i>&comma; Why&comma; because <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>uses <b><i>compareTo&lpar;&rpar; <&sol;i><&sol;b>method to compare keys and <i><b>compareTo&lpar;&rpar; <&sol;b><&sol;i>will throw<b><i> java&period;lang&period;NullPointerException <&sol;i><&sol;b>as shown in below&nbsp&semi;<br &sol;>&NewLine;example &colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;util&period;&ast;&semi; &NewLine; &NewLine;public class TreeSetHashSet &lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; HashSet&lt&semi;String&gt&semi; hashSet &equals; new HashSet&lt&semi;String&gt&semi;&lpar;&rpar;&semi; &NewLine; hashSet&period;add&lpar;"dineshonjava"&rpar;&semi; &NewLine; hashSet&period;add&lpar;null&rpar;&semi; &NewLine; &NewLine; TreeSet&lt&semi;String&gt&semi; treeSet &equals; new TreeSet&lt&semi;String&gt&semi;&lpar;&rpar;&semi; &NewLine; treeSet&period;add&lpar;"dineshonjava"&rpar;&semi; &NewLine; treeSet&period;add&lpar;null&rpar;&semi; &sol;&sol;Java&period;lang&period;NullPointerException &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;differ&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;3&rpar; Another significant difference between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>is that &comma; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><b><i>HashSet <&sol;i><&sol;b><&sol;a>is backed by <b><i>HashMap<&sol;i><&sol;b> while <i><b>TreeSet <&sol;b><&sol;i>is backed by <i><b>TreeMap <&sol;b><&sol;i>in Java&period;<&sol;p>&NewLine;<p> 4&rpar; One more difference between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>which is worth remembering is that <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><b><i>HashSet <&sol;i><&sol;b><&sol;a>uses <i><b>equals&lpar;&rpar; <&sol;b><&sol;i>method to compare two object in Set and for detecting duplicates while <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>uses<b><i> compareTo&lpar;&rpar;<&sol;i><&sol;b> method for same purpose&period; if <i><b>equals&lpar;&rpar;<&sol;b><&sol;i> and <b><i>compareTo&lpar;&rpar;<&sol;i><&sol;b> are not consistent&comma; i&period;e&period; for two equal object equals should return true while <i><b>compareTo&lpar;&rpar;<&sol;b><&sol;i> should return zero&comma; than it will break contract of Set interface and will allow duplicates in <b><i>Set <&sol;i><&sol;b>implementations like <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet<&sol;b><&sol;i><&sol;a><&sol;p>&NewLine;<p> 5&rpar; Now most important difference between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>is ordering&period; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><b><i>HashSet <&sol;i><&sol;b><&sol;a>doesn&&num;8217&semi;t guaranteed any order while <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>maintains objects in Sorted order defined by either <i><b>Comparable <&sol;b><&sol;i>or <b><i>Comparator<&sol;i><&sol;b> method in Java&period;<br &sol;>&NewLine;<b>What is common in <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i>HashSet <&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i>TreeSet <&sol;i><&sol;a>in Java<&sol;b><br &sol;>&NewLine;As I said there are lot of things which are common between <b><i>HashSet <&sol;i><&sol;b>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>in Java&comma; let’s&nbsp&semi;<br &sol;>&NewLine;have a look &colon;<&sol;p>&NewLine;<p> 1&rpar;Both <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><b><i>TreeSet <&sol;i><&sol;b><&sol;a>implements <b><i>java&period;util&period;Set<&sol;i><&sol;b> interface which means they follow contract of <i><b>Set<&sol;b><&sol;i> interface and doesn&&num;8217&semi;t allow any duplicates&period;<&sol;p>&NewLine;<p> 2&rpar;Both <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><i><b>HashSet <&sol;b><&sol;i><&sol;a>and <i><b>TreeSet <&sol;b><&sol;i>are not thread-safe and not synchronized&period; Though you can make them synchronized by using <i><b>Collections&period;synchronizedSet&lpar;&rpar;<&sol;b><&sol;i> method&period;<&sol;p>&NewLine;<p> 3&rpar; Third similarity between <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;"><i><b>TreeSet <&sol;b><&sol;i><&sol;a>and <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;"><b><i>HashSet <&sol;i><&sol;b><&sol;a>is that&comma; Iterator of both classes are fail-fast in nature&period; They will throw <i><b>ConcurrentModificationException <&sol;b><&sol;i>if Iterator is modified once Iterator is created&period; this is not guaranteed and application code should not rely on this code but Java makes best effort to fail as soon as it detects structural change in underlying Set&period;<&sol;p>&NewLine;<p><&sol;p>&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;">&NewLine;<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;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;difference-between-treemap-vs-hashmap&sol;">Difference between TreeMap vs HashMap<&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;<p><&sol;p>&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;treeset-classin-collection&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&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; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;map-interface-in-collection&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&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;treeset-classin-collection&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;map-interface-in-collection&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; '461'&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