The remove() method in Java removes a specific element from a collection, such as a Set, List, or Map. It returns true if the removal was successful.
What is the Set.remove() method in Java?
Key takeaways:
The
Set.remove()method in Java is used to remove a specific element from aSet.It belongs to the
Setinterface, which is implemented by classes likeHashSet,TreeSet, orLinkedHashSet.The method takes one parameter: the object to be removed.
It returns
trueif the set changes after the removal orfalseif no change occurs.This method is useful for dynamically managing sets by removing unwanted elements.
Example: If a
Setcontains[1, 8, 5], callingset.remove(8)updates it to[1, 5].
The Set.remove() method
The Set.remove() method is present in the Set interface inside the java.util package. This method removes only the specified element from the Set. Let’s understand with the help of an example. Suppose that the Set1 contains the following: [1, 8, 5, 3, 0]. We execute the below statements on this set:
Set1.remove(8);Set1.remove(0);
After using the remove() method, the Set1 contains [1, 5, 3].
Syntax
public boolean remove(Object obj)
Parameters
The Set.remove() has one parameter:
obj: It is the element (of typeObject) that needs to be removed from theSet.The
Setinterface is implemented by classes likeHashSet,TreeSet, andLinkedHashSet, but theremove()method works on individual elements, not entire sets.
Return value
The Set.remove() method returns true if the Set gets changed.
Code implementation
Let’s look at a code example below:
import java.util.Set;import java.util.HashSet;class Main{public static void main(String args[]){Set<Integer> NewSet = new HashSet<Integer>();NewSet.add(1);NewSet.add(8);NewSet.add(5);NewSet.add(3);NewSet.add(0);System.out.println("set1 before calling remove(): "+ NewSet);boolean removeVal1;boolean removeVal2;removeVal1 = NewSet.remove(150);removeVal2 = NewSet.remove(8);System.out.println("set1 after calling remove(): "+ NewSet);System.out.println("the removal of 8 was: " + removeVal2);System.out.println("the removal of 150 was: " + removeVal1);}}
Explanation
Line 8: We declare a
Setof integer type:set1. The object is created from theHashSetclass. This is because theSetis an interface in Java and, hence, it cannot be instantiated.Lines 9–13: We add the elements into the
Setusing theSet.add()method.Line 15: We display the
Setbefore calling theremove()method.Lines 19–20: We call the
remove()function to remove specific elements fromSet, and assign the returned values tobooleanvariables. When150passes as the argument, nothing happens. This is because it is not present. However, when8is removed, the change is reflected.Line 21: We display the
Setafter calling theremove()method.Lines 23–24: We display the returned values of the
remove()method.
Conclusion
The Set.remove() method is a powerful tool for managing sets in Java, allowing you to dynamically remove specific elements. It helps keep sets clean and efficient while providing a simple interface for modification. Understanding its syntax, return values, and practical use cases is essential for Java developers working with collections.
Unlock the full potential of Java programming with the “Learn Java” course—your ultimate guide to mastering the fundamentals and building robust applications!
Frequently asked questions
Haven’t found what you were looking for? Contact Us