java linked hash set code example

Example: 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);
   }
}

Tags:

Java Example