LinkedHashSet in java code example

Example 1: how to declare a linked list in java

LinkedList<String> list=new LinkedList<String>();

Example 2: LinkedHashSet in java

import java.util.LinkedHashSet;
public class LinkedHashsetExample
{
   public static void main(String[] args)
   {
      LinkedHashSet<String> lhs = new LinkedHashSet<String>();
      // add elements to LinkedHashSet
      lhs.add("Anteater");
      lhs.add("Bear");
      lhs.add("Cheetah");
      lhs.add("Deer");
      // will not add new element as Anteater already exists
      lhs.add("Anteater");
      lhs.add("Elephant");
      System.out.println("LinkedHashSet size: " + lhs.size());
      System.out.println("Given LinkedHashSet: " + lhs);
      System.out.println("Deer removed from LinkedHashSet: " + lhs.remove("Deer"));
      System.out.println("Remove Zebra which is not present: " + lhs.remove("Zebra"));
      System.out.println("Check if Anteater is present: " + lhs.contains("Anteater"));
      System.out.println("New LinkedHashSet: " + lhs);
   }
}

Example 3: java linkedhashset initialize

LinkedHashSet<Integer> lsh=new LinkedHashSet();
lsh.add(1);

Example 4: what is linkedhashset

- LinkedHashSet can have null and keeps the order

Tags:

Java Example