The LinkedHashSet class in Java is a part of the Java Collections Framework and implements the Set interface.
It is a collection that maintains a linked list of the entries in the set, in the order in which they were inserted. This allows
for predictable iteration order, which is not guaranteed by a regular HashSet.
This guide covers various methods available in the LinkedHashSet class, offering a comprehensive understanding of
how to manipulate and interact with sets in Java. These methods are essential for efficient coding practices and help
in performing operations like adding, removing, and checking for elements.
For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Collections Tutorial.
LinkedHashSet Class Methods
| Method | Description |
|---|---|
| add() | Adds an element to the LinkedHashSet if it is not already present. |
| clear() | Removes all elements from the LinkedHashSet. |
| contains() | Checks if the LinkedHashSet contains a specified element. |
| isEmpty() | Checks if the LinkedHashSet is empty. |
| iterator() | Returns an iterator over the elements in the LinkedHashSet. |
| remove() | Removes a specified element from the LinkedHashSet. |
| size() | Returns the number of elements in the LinkedHashSet. |
| addAll() | Adds all elements from another collection to the LinkedHashSet. |
| addFirst() (Introduced in Java 21) | Adds an element at the beginning of the LinkedHashSet. |
| addLast(E e) (Introduced in Java 21) | Adds an element at the end of the LinkedHashSet. |
| getFirst() (Introduced in Java 21) | Returns the first element of the LinkedHashSet. |
| getLast() (Introduced in Java 21) | Returns the last element of the LinkedHashSet. |
| newLinkedHashSet() (Introduced in Java 21) | Creates a new, empty LinkedHashSet. |
| removeFirst() (Introduced in Java 21) | Removes the first element of the LinkedHashSet. |
| removeLast() (Introduced in Java 21) | Removes the last element of the LinkedHashSet. |
| reversed() (Introduced in Java 21) | Returns a reversed order view of the LinkedHashSet. |
| spliterator() | Creates a Spliterator over the elements in the LinkedHashSet. |
| forEach() | Performs an action for each element in the LinkedHashSet. |
| retainAll() | Keeps only the elements that are also in another collection in the LinkedHashSet. |
| containsAll() | Checks if the LinkedHashSet contains all elements from another collection. |
| removeAll() | Removes all elements that are also in another collection from the LinkedHashSet. |
| stream() | Returns a stream of the elements in the LinkedHashSet. |
| parallelStream() | Returns a parallel stream of the elements in the LinkedHashSet. |
| toArray() | Returns an array containing all elements in the LinkedHashSet. |
References: