LinkedHashSet in Java

LinkedHashSet in Java is the class, Which implements the Set interface in Java.

 

Set In Java:

A Set is a collection that contains unique records only. The set can’t contain the Duplicate Records. In provides three general purpose implementations in Java.

  • HashSet
  • TreeSet
  • LinkedHashSet

 

LikedHashSet in Java

The LinkedHashSet class is the simplest implementation of the Set interface. The LinkedHashSet does not add any additional methods beyond those found in the Set interface. The LinkedHashSet achieves good performance by using a hash to store the Object in the Set. The hash allows fast lookup.

Java HashSet does not guarantee any insertion orders but LinkedHashSet maintains the insertion order. Elements get sorted in the same sequence in which they have been added to the Set.

 

Let’s understand the implementation of LinkedHashSet with an easy example.

Output:

Values of HashSet is — [John, Anshul, Mirilla, Eden, Matt, null]

 

In the above example, First we have declared the LinkedHashSet named lset, and then add some elements in the LinkedHashSet.

Have you noticed we add duplicate values as well but in output we are getting only unique values. One more thing the values stored in the LinkedHashSet is in the same order as they inserted.

We have seen, How we can declare and implement the LinkedHashSet in Java.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

0 Comment

Leave a Reply