Tuesday, December 10, 2019

Hashset Vs Treeset Inwards Java? Similarities In Addition To Differences

HashSet in addition to TreeSet both implement same interface i.e  java.util.Set interface in addition to they receive the character of Set interface agency duplicate elements are non allowed. Both HashSet in addition to TreeSet are used for to shop unique elements, precisely HashSet doesn't tending most whatsoever lodge in addition to TreeSet keeps a affair inwards order. Ordering or sorting on TreeSet tin live customized yesteryear using Comparator interface, yesteryear default TreeSet uses elements natural lodge for sorting, which is defined yesteryear compareTo() method of java.lang.Comparable interface. What is the divergence betwixt HashSet in addition to TreeSet is is besides 1 the frequently asked Java interview question, So you lot should know most similarities in addition to divergence betwixt them? It besides helps you lot to sympathise when to utilization both TreeSet in addition to HashSet in addition to what are the scenario when nosotros should utilization this sets. Let's acquire through the similarities in addition to difference betwixt HashSet in addition to TreeSet inwards Java.



Similarities betwixt TreeSet in addition to HashSet

Here are about similarities betwixt these ii implementations of java.util.Set interface from Collection framework.


Set Interface
Both HashSet in addition to TreeSet implements Set interface.

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

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

Synchronization
Both classes are non shared betwixt multiple threads in addition to non synchronized.

Iterator
Iterator of both TreeSet in addition to HashSet are failed fast.They volition throw ConcurrentModificationException if  the Set is modified in 1 trial the iteration begins.

Cloneable in addition to Serializable
Both classes implement Cloneable in addition to Serializable interface, which agency you lot tin serialize a TreeSet in addition to HashSet in addition to relieve int into the disk or post over the network to other JVM.


You tin besides banking concern represent Java Programming Interview Exposed yesteryear 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 in addition to TreeSet both implement same interface i HashSet vs TreeSet inwards Java? Similarities in addition to Differences


Difference betwixt HashSet in addition to TreeSet inwards Java

Now let's encounter about differences betwixt these ii classes, this volition assist you lot to determine when to utilization TreeSet in addition to HashSet cast inwards Java.


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


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

But inwards the illustration of TreeSet, the lodge of elements are defined yesteryear supplied Comparator, in addition to if you lot don't plough over whatsoever Comparator for TreeSet objects  then it volition utilization natural ordering of elements e.g. Integers inwards their numeric lodge or String inwards their alphabetic order.


Null Value
HashSet allows maximum  of 1 zip element, which agency you lot tin shop alone 1 zip value within HashSet. But TreeSet won't allow whatsoever null object in addition to you lot volition acquire NullPointerException if you lot endeavour to shop zip values inwards TreeSet. Why? because TreeSet sorts chemical component division every bit presently every bit you lot add together it into TreeSet, if the object is zip hence calling compareTo() on that volition throw NullPointerException.


Comparison method
HashSet uses equal() in addition to hashcode() methods to compare the elements, spell TreeSet nosotros tin implements compareTo() method of Comparator interface hence nosotros accept compare() in addition to compareTo() method ,TreeSet  does non utilization equal() in addition to  hashcode() method.


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

TreeSet performance is less every bit compared to HashSet every bit TreeSet has to form the chemical component division later on each insertion in addition to removal Operation. TreeSet offers log(n) time terms for dd, remove, in addition to contains  operations.


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

 HashSet in addition to TreeSet both implement same interface i HashSet vs TreeSet inwards Java? Similarities in addition to Differences


So the determination hither is which cast nosotros should utilization is totally depends on upon our nee. If nosotros desire to form the chemical component division according to our demand utilization Comparator in addition to this is possible inwards TreeSet alone precisely  if nosotros are looking for fast in addition to meliorate performance nosotros should acquire for HashSet in addition to besides if nosotros don't desire to maintain order. TreeSet uses Red- Black tree algorithm underneath to form out the elements. When 1 demand to perform read/write operations frequently, hence TreeSet is non a practiced choice.

So that's it most HashSet in addition to TreeSet inwards Java.

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

No comments:

Post a Comment