<div dir="ltr" style="text-align: justify;" trbidi="on">Several difference between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet </b></i></a>are similar to what we discussed as difference between <i><b>TreeMap </b></i>and <i><b>HashMap</b></i>. Anyway Set and Map are two completely different interface so we will revisit those differences here. Probably most important difference between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet </b></i></a>is the performance. <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>is faster than <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet </b></i></a>which means if you need performance use <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>but <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>doesn&#8217;t provide any kind of ordering so if you need ordering then you need to switch to <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet</b></i></a> which provides sorting of keys. Sorting can be natural order defined by Comparable interface or any particular order defined by Comparator interface in Java. Apart from differences between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet </b></i></a>there are some common things between them. let&#8217;s see what is common between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet </b></i></a>in Java.<br />
1) First major difference between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>is performance. <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>is faster than <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet</i></b></a> and should be preferred choice if sorting of element is not required.</p>
<div align='center' id="ads-id"></div>
<p>2) Second difference between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <i><b>TreeSet </b></i>is that <a href="https://dineshonjava.com/hashset-class-in-collection/"><b><i>HashSet </i></b></a>allows null object but <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>doesn&#8217;t allow null Object and throw <i><b>NullPointerException</b></i>, Why, because <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>uses <b><i>compareTo() </i></b>method to compare keys and <i><b>compareTo() </b></i>will throw<b><i> java.lang.NullPointerException </i></b>as shown in below ;<br />
example :</p>
<pre class="highlight" name="code">import java.util.*; 
 
public class TreeSetHashSet { 
 
 public static void main(String args[]) { 
 HashSet<;String>; hashSet = new HashSet<;String>;(); 
 hashSet.add("dineshonjava"); 
 hashSet.add(null); 
 
 TreeSet<;String>; treeSet = new TreeSet<;String>;(); 
 treeSet.add("dineshonjava"); 
 treeSet.add(null); //Java.lang.NullPointerException 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/differ.png"/></div>
<p>
3) Another significant difference between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>is that , <a href="https://dineshonjava.com/hashset-class-in-collection/"><b><i>HashSet </i></b></a>is backed by <b><i>HashMap</i></b> while <i><b>TreeSet </b></i>is backed by <i><b>TreeMap </b></i>in Java.</p>
<p> 4) One more difference between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>which is worth remembering is that <a href="https://dineshonjava.com/hashset-class-in-collection/"><b><i>HashSet </i></b></a>uses <i><b>equals() </b></i>method to compare two object in Set and for detecting duplicates while <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>uses<b><i> compareTo()</i></b> method for same purpose. if <i><b>equals()</b></i> and <b><i>compareTo()</i></b> are not consistent, i.e. for two equal object equals should return true while <i><b>compareTo()</b></i> should return zero, than it will break contract of Set interface and will allow duplicates in <b><i>Set </i></b>implementations like <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet</b></i></a></p>
<p> 5) Now most important difference between <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>is ordering. <a href="https://dineshonjava.com/hashset-class-in-collection/"><b><i>HashSet </i></b></a>doesn&#8217;t guaranteed any order while <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>maintains objects in Sorted order defined by either <i><b>Comparable </b></i>or <b><i>Comparator</i></b> method in Java.<br />
<b>What is common in <a href="https://dineshonjava.com/hashset-class-in-collection/"><i>HashSet </i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><i>TreeSet </i></a>in Java</b><br />
As I said there are lot of things which are common between <b><i>HashSet </i></b>and <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>in Java, let’s ;<br />
have a look :</p>
<p> 1)Both <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <a href="https://dineshonjava.com/treeset-classin-collection/"><b><i>TreeSet </i></b></a>implements <b><i>java.util.Set</i></b> interface which means they follow contract of <i><b>Set</b></i> interface and doesn&#8217;t allow any duplicates.</p>
<p> 2)Both <a href="https://dineshonjava.com/hashset-class-in-collection/"><i><b>HashSet </b></i></a>and <i><b>TreeSet </b></i>are not thread-safe and not synchronized. Though you can make them synchronized by using <i><b>Collections.synchronizedSet()</b></i> method.</p>
<p> 3) Third similarity between <a href="https://dineshonjava.com/treeset-classin-collection/"><i><b>TreeSet </b></i></a>and <a href="https://dineshonjava.com/hashset-class-in-collection/"><b><i>HashSet </i></b></a>is that, Iterator of both classes are fail-fast in nature. They will throw <i><b>ConcurrentModificationException </b></i>if Iterator is modified once Iterator is created. 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.</p>
<p></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<b>Java Collections Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/arraylist-class-in-java-collection/">ArrayList class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedlist-class-in-collection/">LinkedList class</a></b></li>
<li><b><a href="https://dineshonjava.com/listiterator-interface-in-collection/">ListIterator interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashset-class-in-collection/">HashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashset-class-in-collection/">LinkedHashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/treeset-classin-collection/">TreeSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/map-interface-in-collection/">Map interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashmap-class-in-collection-framework/">HashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/how-does-java-hashmap-work-internally/">How does work HashMap?</a> </b></li>
<li><b><a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">Difference between TreeMap vs HashMap</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">LinkedHashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/treemap-class-in-collection-framework/">TreeMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Hashtable class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashmap-and/">Difference between HashMap and HashTable in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/sorting-in-collection-framework/">Sorting</a></b></li>
<li><b><a href="https://dineshonjava.com/java-comparable-and-comparator/">Comparable interface</a></b></li>
</ol>
</div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/treeset-classin-collection/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/map-interface-in-collection/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/treeset-classin-collection/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/map-interface-in-collection/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '461'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…