LinkedHashSet in java

Let’s learn LinkedHashSet in java.

Linkedhashset in java

LinkedHashSet is a child class of HashSet. It was introduced in java 1.4 version. LinkedHashSet is used when there is a group of individual objects where duplicates are not allowed and insertion order is preserved.

Underlying datastructure of LinkedHashSet is a combination of HashTable and LinkedList.

Hierarchy

linkedhashset in java

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(null);
      lhs.add(15);
      System.out.println(lhs.add("Bear"));
      System.out.println(lhs);
   }
}

Output:

false
[Cheetah, Zebra, Bear, Deer, null, 15]


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