LinkedHashSet in java

Let’s learn LinkedHashSet in java.

linkedhashset in java

LinkedHashSet class implementation is HashTable and LinkedList of Set interface. LinkedHashSet class inherits HashSet class and implements Set interface.

Internally LinkedHashSet class maintain doubly linked list for all elements. Also LinkedHashSet class provide functionality of HashTable and LinkedList data structure.

Hierarchy – LinkedHashSet class

linkedhashset in java

In the above image LinkedHashSet class extends HashSet class which implements Set interface. The Set interface inherits Collection and Iterable interfaces.

LinkedHashSet class have only unique elements and it maintains insertion order. LinkedHashSet class allows null elements and it is non-synchronized.

Syntax:

public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable

E – the type of elements maintained by this set

Implemented Interfaces: Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>.


Now let’s see linkedhashset example.

import java.util.LinkedHashSet;
public class LinkedHashsetExample
{
   public static void main(String[] args)
   {
      LinkedHashSet lhs = new LinkedHashSet();
      lhs.add("Cheetah");    
      lhs.add("Zebra");    
      lhs.add("Bear");   
      lhs.add("Deer");  
      lhs.add("Anteater");
      Iterator iterate = lhs.iterator();  
      while(iterate.hasNext())  
      {  
         System.out.println(iterate.next());  
      }
   }
}


Output:

Cheetah
Zebra
Bear
Deer
Anteater


Let’s see another example of LinkedHashSet class ignoring duplicate elements.

import java.util.Iterator;
import java.util.LinkedHashSet;
public class LinkedHashsetExample 
{
   public static void main(String[] args) 
   {
      LinkedHashSet<String> lhs = new LinkedHashSet<String>();  
      lhs.add("Cheetah");    
      lhs.add("Zebra");    
      lhs.add("Zebra");   
      lhs.add("Deer");
      lhs.add("Anteater");
      Iterator<String> iterate = lhs.iterator();  
      while(iterate.hasNext())  
      {  
         System.out.println(iterate.next());  
      }
   }
}


Output:

Cheetah
Zebra
Deer
Anteater


Here are some operations that are performed on LinkedHashSet class.

add() method: is used to add or insert specified element to LinkedHashSet class. Let’s see an example on add() method of LinkedHashSet class.

import java.util.LinkedHashSet;
public class LinkedHashSetAddMethod
{
   public static void main(String[] args)
   {
      LinkedHashSet<String> lhs = new LinkedHashSet<String>();
      // LinkedHashSet maintains insertion order
      lhs.add("hello");
      lhs.add("world");
      lhs.add("java");
      System.out.println("LinkedHashSet add() method : " + lhs);
   }
}


Output:

LinkedHashSet add() method : [hello, world, java]


remove() method: is used to remove specified element from LinkedHashSet class if it is present. Returns true if this set contained the element. Let’s see an example on remove() method of LinkedHashSet class.

import java.util.LinkedHashSet;
public class LinkedHashSetRemoveMethod
{
   public static void main(String[] args)
   {
      LinkedHashSet<String> lhs = new LinkedHashSet<String>();
      lhs.add("hello");
      lhs.add("world");
      lhs.add("java");
      lhs.add("app");
      lhs.add("download");
      lhs.add("jdk");
      System.out.println("Before using remove() method: " + lhs);
      // removing the element app
      lhs.remove("app");
      System.out.println("After using remove() method: " + lhs);
      // returns false if the element is not present
      System.out.println(lhs.remove("JEE"));
   }
}


Output:

Before using remove() method: [hello, world, java, app, download, jdk]
After using remove() method: [hello, world, java, download, jdk]
false


iterator() method: is used to iterate or access through elements of LinkedHashSet class. Returns an iterator over the elements in this set. The elements are returned in no particular order.

Let’s see an example on iterator() method of LinkedHashSet class.

import java.util.Iterator;
import java.util.LinkedHashSet;
public class LinkedHashSetIteratorMethod
{
   public static void main(String[] args)
   {
      LinkedHashSet<String> lhs = new LinkedHashSet<String>();
      lhs.add("hello");
      lhs.add("world");
      lhs.add("java");
      lhs.add("app");
      lhs.add("download");
      lhs.add("jdk");
      // iterating through LinkedHashSet class
      Iterator<String> iterate = lhs.iterator();
      while(iterate.hasNext())
         System.out.print(iterate.next() + ", ");
      System.out.println();  
      // enhanced for loop
      for(String str : lhs)
         System.out.print(str + ", ");
      System.out.println();
   }
}


Output:

hello, world, java, app, download, jdk,
hello, world, java, app, download, jdk,

Also read – operators in java