This example shows how to remove elements from HashSet in Java. The example also shows how to remove objects of a custom class using the remove method.
How to remove elements from HashSet in Java using the remove method?
The remove
method of the HashSet class removes the specified element from the set object.
1 |
public boolean remove(Object o) |
The remove
method returns true if the specified object is found and removed, false otherwise.
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 |
import java.util.HashSet; public class HashSetRemoveExample { public static void main(String[] args) { HashSet<Integer> hsetNumbers = new HashSet<Integer>(); hsetNumbers.add(1); hsetNumbers.add(2); hsetNumbers.add(3); /* * To remove an element from the HashSet, use * the remove method. * * It returns true if the element is found and removed. */ //this will remove element 2 and return true System.out.println( hsetNumbers.remove(2) ); //this will return false as element 4 is not present in the set System.out.println( hsetNumbers.remove(4) ); System.out.println("HashSet contains: " + hsetNumbers ); } } |
Output
1 2 3 |
true false HashSet contains: [1, 3] |
How to remove objects of a custom class from the HashSet?
Let’s first see an example of removing a custom class object from the HashSet.
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 |
import java.util.HashSet; class Student{ int id; public Student(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "StudentID ->" + getId(); } } public class HashSetRemoveExample { public static void main(String[] args) { HashSet<Student> hsetStudents = new HashSet<Student>(); hsetStudents.add(new Student(1)); hsetStudents.add(new Student(2)); hsetStudents.add(new Student(3)); System.out.println( hsetStudents.remove(new Student(2)) ); System.out.println("HashSet contains: " + hsetStudents ); } } |
Output
1 2 |
false HashSet contains: [StudentID ->3, StudentID ->1, StudentID ->2] |
As we can see from the output, even if the Student object with id 2 was present in the HashSet object, the remove
method returned false and it was not removed from the HashSet object.
The HashSet remove
method relies on the equals
method to compare and find the objects to remove. The equals
method is not implemented by the Student custom class, so the equals
method inherited from the Object class is used which compares the object references. Since the references were different, the remove
method could not find the specified object and hence it returned false.
To solve this problem, we need to implement the equals
and hashCode
methods in our Student custom class 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 |
import java.util.HashSet; class Student{ int id; public Student(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "StudentID ->" + 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; Student other = (Student) obj; if (id != other.id) return false; return true; } } public class HashSetRemoveExample { public static void main(String[] args) { HashSet<Student> hsetStudents = new HashSet<Student>(); hsetStudents.add(new Student(1)); hsetStudents.add(new Student(2)); hsetStudents.add(new Student(3)); System.out.println( hsetStudents.remove(new Student(2)) ); System.out.println("HashSet contains: " + hsetStudents ); } } |
Output
1 2 |
true HashSet contains: [StudentID ->1, StudentID ->3] |
As we can see from the output, the remove
method worked as expected this time and removed the specified object from the HashSet.
Tip: Implement the equals
method and hashCode
method properly to solve the problem of HashSet not removing elements.
How to remove all elements from the HashSet?
To remove all elements from the HashSet, we can either use the clear
method or removeAll
method 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 |
HashSet<Integer> hsetNumbers = new HashSet<Integer>(); hsetNumbers.add(1); hsetNumbers.add(2); hsetNumbers.add(3); /* * To remove all elements, use the clear method */ hsetNumbers.clear(); System.out.println("HashSet contains: " + hsetNumbers ); hsetNumbers.add(4); hsetNumbers.add(5); System.out.println("HashSet contains: " + hsetNumbers ); /* * We can also use the removeAll method */ hsetNumbers.removeAll(hsetNumbers); System.out.println("HashSet contains: " + hsetNumbers ); |
Output
1 2 3 |
HashSet contains: [] HashSet contains: [4, 5] HashSet contains: [] |
Please visit how to clear or remove all elements from the HashSet example to know more.
This example is a part of the Java HashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet