How to Delete User Defined Objects from LinkedHashSet?
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.
Input: ["Geeks", "for", "geeks"] DELETE = "geeks" Output: "Geeks" "for" Input: [1, 2, 3, 4, 5] DELETE = 2 Output: [1, 3, 4, 5]
There are two ways by which we can delete elements from LinkedHashSet:
- Using remove() Method
- Using clear() Method
Method 1: remove() methods are used to delete a specific element from a LinkedHashSet object.
Parameters: The parameter O is of the type of LinkedHashSet and specifies the element to be removed from the LinkedHashSet.
Return Value: This method returns True if the specified element is present in the LinkedHashSet otherwise it returns False.
Before deleting an element : [GEEKS, FOR] After deleting an element: [GEEKS]
Method 2: clear() method is used to delete all element from LinkedHashSet. The set will be empty after this call returns.
public void clear()
Return Value: This method does not return anything.
Below are the examples to illustrate the clear() method.
Before deleting an element : [GEEKS, FOR] After deleting all element: 
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.