Difference betwixt HashSet in addition to TreeSet inwards Java

Difference betwixt HashSet together with TreeSet inwards Java
There are several differences betwixt an HashSet and a TreeSet is similar to what nosotros discussed every bit a departure betwixt TreeMap together with HashMap. Anyway, Set together with Map are 2 completely unlike interfaces thence nosotros volition revisit those differences here. Probably the most of import departure between HashSet and TreeSet is the performance. HashSet is faster than TreeSet which agency if you lot require functioning utilisation HashSet only HashSet doesn't render whatever sort of ordering thence if you lot require ordering thence you lot require to switch to TreeSet which provides sorting of keys. Sorting tin survive natural order defined by a Comparable interface or whatever special social club defined yesteryear Comparator interface inwards Java.

Apart from differences betwixt HashSet together with TreeSet, at that topographic point are roughly mutual things betwixt them. let's encounter what is mutual betwixt HashSet together with TreeSet inwards Java. By the way, this is 1 of the popular Java collection interview questions much similar ArrayList vs Vector together with Hashtable vs HashMap. If you lot are going for whatever Java programming interview, it's worth preparing.



What is Common inwards HashSet together with TreeSet inwards Java

As I said at that topographic point are lot of things which are mutual betwixt HashSet together with TreeSet inwards Java, let’s convey a look:


1)Both HashSet together with TreeSet implements java.util.Set interface which agency they follow contract of Set interface together with doesn't allow whatever duplicates.

2)Both HashSet together with TreeSet are non thread-safe together with non synchronized. Though you lot tin brand them synchronized yesteryear using Collections.synchronizedSet() method.

3) The tertiary similarity betwixt TreeSet together with HashSet is that Iterator of both classes is fail-fast in nature. They volition throw ConcurrentModificationException if Iterator is modified 1 time Iterator is created. this is non guaranteed together with application code should non rely on this code only Java makes best try to neglect every bit presently every bit it detects a structural modify inwards underlying Set.



HashSet vs TreeSet inwards Java

Difference betwixt HashSet together with TreeSet inwards Java Difference betwixt HashSet  together with TreeSet inwards JavaNow let's encounter a dyad of differences betwixt HashSet vs TreeSet inwards Java. This is plenty to create upwards one's heed whether you lot should utilisation HashSet or TreeSet inwards a given scenario.

1) The kickoff major departure betwixt HashSet together with TreeSet is performance. HashSet is faster than TreeSet together with should survive preferred selection if sorting of elements is non required. TreeSet is internally backed yesteryear a Red-Black tree. For a detailed description of Red-Black Tree, you lot should read a practiced mass on information construction together with algorithms e.g. Introduction to Algorithms yesteryear Thomas Corman.

Difference betwixt HashSet together with TreeSet inwards Java Difference betwixt HashSet  together with TreeSet inwards Java


The functioning departure comes from the underlying information construction used yesteryear TreeSet together with HashSet i.e. a tree together with a hash table. Adding an chemical component of a tree is slower than adding it to a hash tabular array only it is even thence much faster than adding it into the right house inwards the linked listing or array. If the tree contains n elements, thence an average log2N comparisons are required to disclose the right seat for a novel element. For example, if the tree contains one thousand elements than adding a novel chemical component requires most 10 comparisons.


2) Second departure betwixt HashSet together with TreeSet is that HashSet allows nil object only TreeSet doesn't allow nil Object together with throw NullPointerException, Why, because TreeSet uses compareTo() method to compare keys together with compareTo() volition throw java.lang.NullPointerException every bit shown inwards below event :

HashSet<String> hashSet = new HashSet<String>();
hashSet.add("Java");
hashSet.add(null);
       
TreeSet<String> treeSet = new TreeSet<String>();
treeSet.add("C++");
treeSet.add(null); //Java.lang.NullPointerException
Output:
Exception inwards thread "main" java.lang.NullPointerException
        at java.util.TreeMap.put(TreeMap.java:541)
        at java.util.TreeSet.add(TreeSet.java:238)
        at test.CollectionTest.main(CollectionTest.java:27)
Java Result: 1


3) Another meaning departure betwixt HashSet together with TreeSet is that HashSet is backed yesteryear HashMap field TreeSet is backed by TreeMap inwards Java.


4) One to a greater extent than departure betwixt HashSet together with TreeSet which is worth remembering is that HashSet uses equals() method to compare 2 objects inwards Set together with for detecting duplicates field TreeSet uses compareTo() method for the same purpose. if equals() together with compareTo() are non consistent, i.e. for 2 equal object equals should render truthful field compareTo() should render null thence it volition interruption the contract of Set interface together with volition allow duplicates inwards Set implementations similar TreeSet


5) Now the most of import departure betwixt HashSet together with TreeSet is ordering. HashSet doesn't guarantee whatever social club field TreeSet maintains objects inwards the Sorted social club defined yesteryear either Comparable or Comparator method inwards Java.

Here is a overnice summary slide of commutation differences betwixt TreeSet together with HashSet inwards Java, which compares both of these collections on ordering, sorting, performance, underlying information structure, the method used for duplicate detection together with how they are implemented inwards JDK.



That's all on the difference betwixt HashSet together with TreeSet inwards Java. Use HashSet if you lot don't require sorting together with looking for improve functioning field TreeSet is the kickoff selection if you lot require to hold objects inwards a sorted social club inwards Java.

Further Learning
Java Fundamentals: Collections
From Collections to Streams inwards Java 8 Using Lambda Expressions
Java Programming Interview Exposed yesteryear Makham
How to loop Map inwards Java
how to traverse over ArrayList inwards Java 
How to sort ArrayList inwards ascending social club inwards Java
How to convert ArrayList to HashSet inwards Java
Difference betwixt List together with Set inwards Java

Subscribe to receive free email updates:

0 Response to "Difference betwixt HashSet in addition to TreeSet inwards Java"

Posting Komentar