Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Implement LinkedHashSet API

  • Last Updated : 02 Aug, 2021

The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained, this class is used. When iterating through a HashSet, the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned to the order in which they were inserted.

To implement LinkedHashSet API first, we create a class “LinkedHashSetImplmentation” and create all the methods of the LinkedHashSet in this class.

Implementation of the LinkedHashMap API:

Java




// Java program to implement LinkedHashSet API
 
import java.util.*;
 
class LinkedHashSetImplementation<E> {
    private LinkedHashSet<E> set;
 
    // Constructor creates a new LinkedHashSet
    public LinkedHashSetImplementation()
    {
        set = new LinkedHashSet<E>();
    }
 
    // Constructor creates a new empty linkedhash set
    // according to the given set
    public LinkedHashSetImplementation(
        Collection<? extends E> set1)
    {
        set = new LinkedHashSet<E>(set1);
    }
 
    // Returns the size of the set
    public int size() { return set.size(); }
 
    // Returns true if set is empty otherwise return false
    public boolean isEmpty() { return set.isEmpty(); }
 
    // Returns true if set contains specified value
    public boolean contains(Object val)
    {
        return set.contains(val);
    }
 
    // Returns an iterator over set elements
    public Iterator<E> iterator() { return set.iterator(); }
 
    // Returns an array containing all of the elements of
    // the set
    public Object[] toArray() { return set.toArray(); }
 
    // Returns an array containing all of the elements of
    // the set
    public <T> T[] toArray(T[] a) { return set.toArray(a); }
 
    // Add element to the set
    public boolean add(E ele) { return set.add(ele); }
 
    // Removes the specified element from the set if it is
    // present
    public boolean remove(Object ele)
    {
        return set.remove(ele);
    }
 
    // Returns true if the set contains all of the elements
    // of the specified collection
    public boolean containsAll(Collection<?> c)
    {
        return set.containsAll(c);
    }
 
    // Adds all of the elements in the specified collection
    // to the set
    public boolean addAll(Collection<? extends E> col)
    {
        return set.addAll(col);
    }
 
    // Retains only the elements in this set that are
    // contained in the specified collection
    public boolean retainAll(Collection<?> col)
    {
        return set.retainAll(col);
    }
 
    // Removes from this set all of its elements that are
    // contained in the given collection
    public boolean removeAll(Collection<?> col)
    {
        return set.retainAll(col);
    }
 
    // Removes all of the elements from the set
    public void clear() { set.clear(); }
 
    // Compares the specified object with the set
    public boolean equals(Object obj)
    {
        return set.equals(obj);
    }
 
    // Returns the hash code value for the set
    public int hashCode() { return set.hashCode(); }
}
public class GFG {
    public static void main(String[] arg)
    {
        LinkedHashSetImplementation<Integer> set
            = new LinkedHashSetImplementation<>();
 
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(40);
        set.add(50);
        set.add(30);
        set.add(40);
 
        // Print the size of the set
        System.out.println("Size of the LinkedHashset: "
                           + set.size());
 
        // Iterate set elements
        System.out.println(
            "The LinkedHashSet elements are:");
 
        Iterator it = set.iterator();
 
        while (it.hasNext()) {
            // print set data
            System.out.println(it.next());
        }
 
        // Check whether set is empty or not
        System.out.println("The linkedHashSet is empty: "
                           + set.isEmpty());
 
        // Print true if set contains 60 else print false
        System.out.println("LinkedHashSet contains 60: "
                           + set.contains(60));
 
        // Print true if set contains 40 else print false
        System.out.println("LinkedHashSet contains 40: "
                           + set.contains(40));
 
        // Remove element from Set
        set.remove(40);
 
        // print size
        System.out.println("Size of the linkedHashSet:"
                           + set.size());
 
        // Delete all the elements of the set
        set.clear();
 
        System.out.println("Size of the set after clear:"
                           + set.size());
    }
}

 
 

Output
Size of the LinkedHashset: 5
The LinkedHashSet elements are:
10
20
30
40
50
The linkedHashSet is empty: false
LinkedHashSet contains 60: false
LinkedHashSet contains 40: true
Size of the linkedHashSet:4
Size of the set after clear:0

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :