Java LinkedHashSet

LinkedHashSet in Java with Example

Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order of insertion. It returns the elements in the order in which they are inserted. It also maintains the linked list of the elements in the set. The syntax of the LinkedHashSet is:

It also has two factors which affect the performance of LinkedHashSet-> Load Factor and Initial Capacity.

Difference between HashSet and LinkedHashSet:

Categories

HashSet

LinkedHashSet

Internal Working Internally uses HashMap for storing objects. Internally uses LinkedHashMap for storing objects.
Order Not maintain the insertion order. Maintain the insertion order.
Complexity The complexity is O(1). The complexity is O(1).
Performance Its performance is better than LinkedHashSet. Its performance is slower than HashSet due to the insertion order.
Compare It uses equals () and hashCode (). It also uses equals () and hashCode () for comparing.
Null Elements It allows only one null element. It allows only one null element.
When to use If you want unique elements but don’t want to maintain the order of insertion. It used when to want to maintain the insertion order of the elements.
Syntax HashSet<String> hs= new HashSet<String> (); LinkedHashSet<String> h= new LinkedHashSet<String> ();

 Similarities between HashSet and LinkedHashSet:

  1. The HashSet and LinkedHashSet implements Set interface. Hence, they are not allowed to keep the duplicate value.
  2. HashSet and LinkedHashSet both are cloneable and Serializable.
  3. You have to make it externally synchronize if you want to use multi-threading environment because both do not thread safe.

Time Complexity of LinkedHashSet

LinkedHashSet uses the HashTable Data Structure. The Complexity of the LinkedHashSet is O(1) time for adding, removing, and retrieving operations.

Constructors of LinkedHashSet

  1. LinkedHashSet (): It is used to create the default HashSet.
  2. LinkedHashSet (Collection c): It is used to initialize with elements of the collection.
  3. LinkedHashSet (int size): It is used to initialize the size of LinkedHashSet.
  4. LinkedHashSet (Int Capacity, float Loadfactor): This is used to initialize both the capacity and load factor.

Methods of LinkedHashSet

  1. add (object o): This method is used to add the elements in HashSet.

Example:

Output:

  1. remove (object o): Used to remove the elements from the set.

Example:

Output:

After removing:

  1. contains (object o): It returns true if the element found in the set.

Example:

Output:

Does the set contain 67? true

Does the set contain 99? true

Does the set contain 88? false

  1. isEmpty (): It returns true if the set is empty otherwise false.

Example:

Output:

Is the set empty: false

Is the set empty: true

  1. int size (): This method is used to return the number of elements in the set.

Example:

Output:

The size of the element: 5

Set contain: 0 elements

  1. retainAll (): It is used to retain the entire element from the collection and show only those elements that are same in both Sets.

Example:

Output:

The element before retainAll operation:

The element for retain:

The elements after retainAll operation:

  1. hashCode (): It is used to get the hashcode value of the instance of the LinkedHashSet.

Example:

Output:

The element:

The hashcode value:

Difference between LinkedHashSet and LinkedHashMap:

LinkedHashSet

LinkedHashMap

1. It is used to store the collection of elements. 1. It is used to store the key-value pairs.
2. It implements HashSet. 2. It Implements HashMap.
3. It doesn’t store duplicates values. 3. It stores unique keys but can store duplicate values.
4. The syntax and to add the elements in the Set is:

LinkedHashSet<String> lhs= new LinkedHashSet<String> ();

lhs.add(“Welcome”);

lhs.add(“to”);

lhs.add(“TutorialAndExample”);

4.The syntax and the add the elements in the map is:

LinkedHashMap<String> lhm= new LinkedHashMap<String> ();

lhm.put (“Welcome”,5);

lhm.put (“to”,6);

lhm.put(“TutorialAndExample”,12);

 Example using iterator() method

Output:

The element: