How to Delete User Defined Objects from LinkedHashSet?
Last Updated :
28 Jun, 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.
Example:
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.
Syntax:
LinkedHashSet.remove(Object O)
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.
Java
// Java program to delete user defined
// objects from LinkedHashSet
import java.util.*;
public class GFG {
public static void main(String[] args)
{
LinkedHashSet<String> lset
= new LinkedHashSet<String>();
// add elements to HashSet
lset.add("GEEKS");
lset.add("FOR");
lset.add("GEEKS");
// Displaying the LinkedHashSet Before Deleting
System.out.println("Before deleting an element :");
System.out.println(lset);
// remove() method to delete an element from
// LinkedHashSet
lset.remove("FOR");
// Displaying the LinkedHashSet After Deleting
System.out.println("After deleting an element:");
System.out.println(lset);
}
}
OutputBefore 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.
Syntax:
public void clear()
Return Value: This method does not return anything.
Below are the examples to illustrate the clear() method.
Java
// Java program to delete elements
// from LinkedHashSet
import java.util.*;
import java.util.LinkedHashSet;
public class GFG {
public static void main(String[] args)
{
LinkedHashSet<String> l_set
= new LinkedHashSet<String>();
// add elements to HashSet
l_set.add("GEEKS");
l_set.add("FOR");
l_set.add("GEEKS");
// Displaying the LinkedHashSet Before Deleting
System.out.println("Before deleting an element :");
System.out.println(l_set);
// clear() method to delete all element from
// LinkedHashSet
l_set.clear();
// Displaying an empty LinkedHashSet After Deleting
System.out.println("After deleting all element:");
System.out.println(l_set);
}
}
OutputBefore deleting an element :
[GEEKS, FOR]
After deleting all element:
[]
Similar Reads
How to Find User Defined Objects From LinkedHashSet in Java? LinkedHashSet is used to store elements in which order they were inserted. 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 i
3 min read
How to Eliminate Duplicate User Defined Objects from LinkedHashSet in Java? While creating a HashSet of your own class, always ensure that the HashCode() method of the key of HashSet doesnât change. Java Object hashCode() is a native method and returns the integer hash code value of the object. If two objects are equal according to the equals() method, then their hash code
3 min read
How to Eliminate Duplicate User Defined Objects as a Key from Java LinkedHashMap? Duplicate user-defined objects as a key from Java LinkedHashMap can be removed and achieved by implementing equals and hashcode methods at the user-defined objects. Example: Input : LinkedHashMap = [{[Apple, 40], Kashmir}, {[Grapes, 80], Nashik}] Duplicate key = {[Grapes, 80], Delhi} Output: LinkedH
2 min read
How to Create a LinkedHashSet with Custom Equality for Objects? In this article, we want to create a LinkedHashSet of custom objects with specific equality criteria. So, we can create it by overriding the equals and hashcode methods in Java. In the equals() method, we can put the custom criteria over there and we override the hashcode to provide the same Hashcod
3 min read
How to Remove Elements from a LinkedHashMap in Java? A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove
2 min read