Java LinkedHashSet Class Methods

The LinkedHashSet class in Java is part of the java.util package and is a subclass of HashSet. It maintains a linked list of the entries in the set, in the order in which they were inserted, thus providing predictable iteration order. This is useful when you need to maintain the order of elements while avoiding duplicates.

This guide covers various methods available in the LinkedHashSet class. Each method is described in simple terms to help beginners understand how to use them. These methods allow you to perform operations like adding, removing, and checking for elements in the set while preserving their order.

For more detailed information, please refer to the official Java SE Documentation and additional resources on Java Collections Tutorial. Also, check out this comprehensive LinkedHashSet Guide for more examples and usage scenarios.

Java LinkedHashSet Class Methods

The table below contains various methods of the Java LinkedHashSet class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
add() Adds the specified element to this set if it is not already present.
clear() Removes all of the elements from this set.
contains() Returns true if this set contains the specified element.
isEmpty() Returns true if this set contains no elements.
iterator() Returns an iterator over the elements in this set.
remove() Removes the specified element from this set if it is present.
size() Returns the number of elements in this set.
addFirst() Adds the specified element to the beginning of this set. (Introduced in Java 21)
addLast() Adds the specified element to the end of this set. (Introduced in Java 21)
getFirst() Returns the first element in this set. (Introduced in Java 21)
getLast() Returns the last element in this set. (Introduced in Java 21)
newLinkedHashSet() Creates a new LinkedHashSet instance. (Introduced in Java 21)
removeFirst() Removes and returns the first element from this set. (Introduced in Java 21)
spliterator() Creates a late-binding and fail-fast Spliterator over the elements in this set.
addAll() Adds all of the elements in the specified collection to this set.
removeLast() Removes and returns the last element from this set. (Introduced in Java 21)
reversed() Returns a reversed view of this set. (Introduced in Java 21)
containsAll() Returns true if this set contains all of the elements in the specified collection.
retainAll() Retains only the elements in this set that are contained in the specified collection.
removeAll() Removes from this set all of its elements that are contained in the specified collection.
toArray() Returns an array containing all of the elements in this set.
parallelStream() Returns a possibly parallel Stream with this set as its source.
stream() Returns a sequential Stream with this set as its source.
forEach() Performs the given action for each element of this set until all elements have been processed or the action throws an exception.

Comments