Java Program to Implement LinkedHashSet API Last Updated : 02 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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()); } } OutputSize 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 Comment More infoAdvertise with us Next Article Java Program to Implement LinkedHashSet API N nikhilchhipa9 Follow Improve Article Tags : Java Java Programs Java-Collections java-LinkedHashSet Practice Tags : JavaJava-Collections Similar Reads Java Program to Implement LinkedHashMap API The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements 3 min read Java Program to Implement LinkedList API Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa 10 min read Java Program to Implement LinkedTransferQueue API LinkedTransferQueue is a queue that orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time fo 5 min read Java Program to Iterate LinkedHashSet Elements 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 3 min read Java Program to Get Elements By Index from LinkedHashSet LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde 4 min read How to Print LinkedHashSet Elements in Java? LinkedHashSet is a child class of HashSet in which duplicates are not allowed but the insertion order is preserved. The elements are printed in the same order in which they were inserted. There are several ways to print LinkedHashSet elements: By simply printing the elementsBy using enhanced for loo 4 min read Java Program to Implement HashTable API The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. To implement Hashtable API f 4 min read How to Get Random Elements from LinkedHashSet in Java? LinkedHashSet is used to maintain the insertion order and for generating random elements from LinkedHashSet we will use Random Class to generate a random number between 0 and the LinkedHashSet size. That random number will act as the index of LinkedHashSet. We can get a random element in three ways: 3 min read How to Sort LinkedHashSet Elements in Descending Order in Java? 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 iteration is through the elements in 2 min read Java Program to Implement Hash Tables Chaining with Doubly Linked Lists Hash Tables(similar to tables in general) provide a subset of the dynamic set operations. Usually, a set of keys are mapped with some values based on certain relations. However, there might be situations when different keys map to the same position provided by the Hash function, which leads to a col 5 min read Like