This example shows how to remove an element from the LinkedHasSet in Java. This example also shows how to remove an element from LinkedHashSet of custom class objects.
How to remove an element from LinkedHashSet in Java?
The remove
method of the LinkedHashSet class removes the specified element from the LinkedHashSet object.
1 |
public boolean remove(Object object) |
The remove
method returns true if the specified element exists in the LinkedHashSet and it was removed. If the specified object is not found in the set, it returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.LinkedHashSet; public class LinkedHashSetRemoveExample { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); /* * To remove an element from the LinkedHashSet, use * the remove method */ //this will remove "red" and return true System.out.println( lhSetColors.remove("red") ); //this will return false as "white" element does not exist System.out.println(lhSetColors.remove("white")); System.out.println("LinkedHashSet contains: " + lhSetColors); } } |
Output
1 2 3 |
true false LinkedHashSet contains: [green, blue] |
How to remove objects of a custom class from the LinkedHashSet?
The remove
method relies on the equals
method to find and remove the element from the LinkedHashSet object. If the custom class has not overridden the equals
and hashCode
methods, the remove
method does not work properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.LinkedHashSet; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } } public class LinkedHashSetRemoveExample { public static void main(String[] args) { LinkedHashSet<User> lhSetUsers = new LinkedHashSet<User>(); lhSetUsers.add( new User(1) ); lhSetUsers.add( new User(2) ); lhSetUsers.add( new User(3) ); //remove an element System.out.println( lhSetUsers.remove( new User(2) ) ); System.out.println("LinkedHashSet contains: " + lhSetUsers); } } |
Output
1 2 |
false LinkedHashSet contains: [User -> 1, User -> 2, User -> 3] |
As we can see from the output, the object was not removed even if it was present in the LinkedHashSet. To solve the problem of custom class not being removed from the LinkedHashSet, the custom class needs to override the equals
and hashCode
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import java.util.LinkedHashSet; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (id != other.id) return false; return true; } } public class LinkedHashSetRemoveExample { public static void main(String[] args) { LinkedHashSet<User> lhSetUsers = new LinkedHashSet<User>(); lhSetUsers.add( new User(1) ); lhSetUsers.add( new User(2) ); lhSetUsers.add( new User(3) ); //remove an element System.out.println( lhSetUsers.remove( new User(2) ) ); System.out.println("LinkedHashSet contains: " + lhSetUsers); } } |
Output
1 2 |
true LinkedHashSet contains: [User -> 1, User -> 3] |
Please also visit how to clear or remove all elements from the LinkedHashSet example to know more.
This example is a part of the Java LinkedHashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet