LinkedHashSet class

The LinkedHashSet class extends HashSet and adds no members of its own. It is a generic class that has this declaration:

class LinkedHashSet<E>

E specifies the type of objects that the set will hold. Its constructors parallel those in HashSet.

LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted.

This allows insertion-order iteration over the set.

Constructor

LinkedHashSet()
Creates a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
LinkedHashSet(Collection<? extends E> c)
Creates a new linked hash set with the same elements as the specified collection.
LinkedHashSet(int initialCapacity)
Creates a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).
LinkedHashSet(int initialCapacity, float loadFactor)
Creates a new, empty linked hash set with the specified initial capacity and load factor.

  import java.util.LinkedHashSet;

public class Main {

  public static void main(String[] args) {
    LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();

    System.out.println("Size of LinkedHashSet : " + lhashSet.size());
    lhashSet.add(new Integer("1"));
    lhashSet.add(new Integer("2"));
    lhashSet.add(new Integer("3"));

    System.out.println(lhashSet.size());

    lhashSet.remove(new Integer("1"));

    System.out.println(lhashSet.size());
  }
}
  

The output:


Size of LinkedHashSet : 0
3
2
Home 
  Java Book 
    Collection  

LinkedHashSet:
  1. LinkedHashSet class
  2. Check if an element exists in LinkedHashSet using contains method
  3. Copy all elements of LinkedHashSet to an Object Array