HashSet vs TreeSet inwards Java? Similarities as well as Differences

HashSet too TreeSet both implement same interface i.e  java.util.Set interface too they own the character of Set interface agency duplicate elements are non allowed. Both HashSet too TreeSet are used for to shop unique elements, exactly HashSet doesn't aid virtually whatever companionship too TreeSet keeps a matter inwards order. Ordering or sorting on TreeSet tin live customized past times using Comparator interface, past times default TreeSet uses elements natural companionship for sorting, which is defined past times compareTo() method of java.lang.Comparable interface. What is the departure betwixt HashSet too TreeSet is is equally good i the frequently asked Java interview question, So yous should know virtually similarities too departure betwixt them? It equally good helps yous to empathise when to work both TreeSet too HashSet too what are the scenario when nosotros should work this sets. Let's become through the similarities too difference betwixt HashSet too TreeSet inwards Java.



Similarities betwixt TreeSet too HashSet

Here are approximately similarities betwixt these 2 implementations of java.util.Set interface from Collection framework.


Set Interface
Both HashSet too TreeSet implements Set interface.

Insertion Order
Both cast don't maintain insertion companionship of elements .

Duplicate value
Both classes don't allow duplicate elements, add() method spend upwards them past times returning false.

Synchronization
Both classes are non shared betwixt multiple threads too non synchronized.

Iterator
Iterator of both TreeSet too HashSet are failed fast.They volition throw ConcurrentModificationException if  the Set is modified in i lawsuit the iteration begins.

Cloneable too Serializable
Both classes implement Cloneable too Serializable interface, which agency yous tin serialize a TreeSet too HashSet too salve int into the disk or post over the network to other JVM.


You tin equally good cheque Java Programming Interview Exposed past times Markham for to a greater extent than of such questions from Java developer interviews. One of a consummate mass for getting prepared for Java JEE interviews.

 HashSet too TreeSet both implement same interface i HashSet vs TreeSet inwards Java? Similarities too Differences


Difference betwixt HashSet too TreeSet inwards Java

Now let's run into approximately differences betwixt these 2 classes, this volition aid yous to create upwards one's hear when to work TreeSet too HashSet cast inwards Java.


Internal Structure
HashSet internally uses HashMap to shop its element, land TreeSet uses TreeMap to shop its chemical component internally. See this article to larn to a greater extent than virtually how HashSet plant inwards Java.


Ordering
HashSet is unordered when nosotros shop objects inwards HashSet it stores too hence inwards random order, agency if nosotros desire to impress the chemical component past times using its Iterator, their companionship does non guarantee that the outset chemical component nosotros inserted volition live printed first.

But inwards the example of TreeSet, the companionship of elements are defined past times supplied Comparator, too if yous don't plough over whatever Comparator for TreeSet objects  then it volition work natural ordering of elements e.g. Integers inwards their numeric companionship or String inwards their alphabetic order.


Null Value
HashSet allows maximum  of i nil element, which agency yous tin shop entirely i nil value within HashSet. But TreeSet won't allow whatever null object too yous volition acquire NullPointerException if yous sweat to shop nil values inwards TreeSet. Why? because TreeSet sorts chemical component equally shortly equally yous add together it into TreeSet, if the object is nil too hence calling compareTo() on that volition throw NullPointerException.


Comparison method
HashSet uses equal() too hashcode() methods to compare the elements, land TreeSet nosotros tin implements compareTo() method of Comparator interface hence nosotros convey compare() too compareTo() method ,TreeSet  does non work equal() too  hashcode() method.


Speed too Performance
HashSet is faster than TreeSet for all full general purpose .The add, take too contains methods has constant fourth dimension complexity O(1)  for HashSet, which agency HashSet offers constant fourth dimension toll for adding, removing too checking if an object exists inwards Set.

TreeSet performance is less equally compared to HashSet equally TreeSet has to variety the chemical component later each insertion too removal Operation. TreeSet offers log(n) time toll for dd, remove, too contains  operations.


Functionality
TreeSet offers sorting which is non provided past times HashSet. HashSet equally good has less methods equally compared to TreeSet. TreeSet is rich inwards functionality equally compare to HashSet. Functions like pollFirst(), pollLast(), first(), last(), ceiling(), lower() etc makes TreeSet rich inwards functionality wise.

 HashSet too TreeSet both implement same interface i HashSet vs TreeSet inwards Java? Similarities too Differences


So the decision hither is which cast nosotros should work is totally depends on upon our nee. If nosotros desire to variety the chemical component according to our demand work Comparator too this is possible inwards TreeSet entirely exactly  if nosotros are looking for fast too improve performance nosotros should become for HashSet too equally good if nosotros don't desire to maintain order. TreeSet uses Red- Black tree algorithm underneath to variety out the elements. When i demand to perform read/write operations frequently, too hence TreeSet is non a skillful choice.

So that's it virtually HashSet too TreeSet inwards Java.

Other interview questions based upon differences of 2 classes inwards Java :
  • What is the departure betwixt Comparator too Comparable inwards Java? [answer]
  • The departure betwixt type 1, 2, iii too iv JDBC driver inwards Java? [answer]
  • The departure betwixt CopyOnWriteArrayList too Synchronized ArrayList inwards Java? [answer]
  • What is the departure betwixt Abstraction too Polymorphism inwards Java? [answer]
  • What is the departure betwixt fail-fast too fail-safe iterator inwards Java? [answer]

Subscribe to receive free email updates:

0 Response to "HashSet vs TreeSet inwards Java? Similarities as well as Differences"

Posting Komentar