Java LinkedHashSet class is a linked list based implementation of Set interface. It is used to store unique elements. It uses hash table internally to store the elements. It implements Set interface and extends the HashSet class. Declaration of the class is given below.
public class LinkedHashSet<E>extends HashSet<E>implements Set<E>, Cloneable, Serializable
LinkedHashSet class has four constructors that can be used to create LinkedHashSet accordingly.
LinkedHashSet() // This creates an empty LinkedHashSet
LinkedHashSet( Collection C ) // This creates a LinkedHashSet that is initialized with the elements of the Collection C
LinkedHashSet( int capacity ) // This creates a LinkedHashSet that has the specified initial capacity
LinkedHashSet( int initialCapacity, float loadFactor )
In this example, we are creating a LinkedHashSet which is initially empty. Later on we will add items to this collection.
import java.util.*;
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating LinkedHashSet
LinkedHashSet<String> hs = new LinkedHashSet<String>();
// Displaying LinkedHashSet
System.out.println(hs);
}
}
[]
It Inherits methods from HashSet class. So we can apply all the HashSet operations with LinkedHashSet. The given table contains the methods of HashSet.
Method | Description |
---|---|
add(E e) | It adds the specified element to this set if it is not already present. |
clear() | It removes all of the elements from the set. |
clone() | It returns a shallow copy of this LinkedHashSet instance: the elements themselves are not cloned. |
contains(Object o) | It returns true if this set contains the specified element. |
isEmpty() | It returns true if this set contains no elements. |
iterator() | It returns an iterator over the elements in this set. |
remove(Object o) | It removes the specified element from this set if it is present. |
size() | It returns the number of elements in the set. |
spliterator() | It creates a late-binding and fail-fast Spliterator over the elements in the set. |
In this example, we are creating a LinkedHashSet that store integer values. Since LinkedHashSet does not store duplicate elements, we tried to add a duplicate elements but the output contains only unique elements.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating LinkedHashSet
LinkedHashSet hs = new LinkedHashSet<>();
// Adding elements
hs.add(100);
hs.add(200);
hs.add(300);
hs.add(100);
// Displaying LinkedHashSet
System.out.println(hs);
}
}
[100, 200, 300]
To remove elements from the LinkedHashSet, we are using remove() method that remove the specified elements.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating LinkedHashSet
LinkedHashSet<Integer> hs = new LinkedHashSet<Integer>();
// Adding elements
hs.add(100);
hs.add(200);
hs.add(300);
hs.add(100);
// Displaying LinkedHashSet
System.out.println(hs);
// Removing elements
hs.remove(300);
System.out.println("After removing elements: \n"+hs);
}
}
[100, 200, 300] After removing elements: [100, 200]
Since LinkedHashSet is a collection then we can use loop to iterate its elements. In this example we are traversing elements using for loop. See the below example.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating LinkedHashSet
LinkedHashSet<Integer> hs = new LinkedHashSet<Integer>();
// Adding elements
hs.add(100);
hs.add(200);
hs.add(300);
hs.add(100);
// Traversing ArrayList
for(Integer element : hs) {
System.out.println(element);
}
}
}
100 200 300
Sometimes we want to know number of elements an LinkedHashSet holds. In that case we use size() then returns size of LinkedHashSet which is equal to number of elements present in the list.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating LinkedHashSet
LinkedHashSet<Integer> hs = new LinkedHashSet<Integer>();
// Adding elements
hs.add(100);
hs.add(200);
hs.add(300);
hs.add(100);
// Traversing ArrayList
for(Integer element : hs) {
System.out.println(element);
}
System.out.println("Total elements : "+hs.size());
}
}
100 200 300 Total elements : 3