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.
Key takeaways:
The Set.remove()
method in Java is used to remove a specific element from a Set
.
It belongs to the Set
interface, which is implemented by classes like HashSet
, TreeSet
, or LinkedHashSet
.
The method takes one parameter: the object to be removed.
It returns true
if the set changes after the removal or false
if no change occurs.
This method is useful for dynamically managing sets by removing unwanted elements.
Example: If a Set
contains [1, 8, 5]
, calling set.remove(8)
updates it to [1, 5]
.
Set.remove()
methodThe 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]
.
public boolean remove(Object obj)
The Set.remove()
has one parameter:
obj
: It is the element (of type Object
) that needs to be removed from the Set
.
The Set
interface is implemented by classes like HashSet
, TreeSet
, and LinkedHashSet
, but the remove()
method works on individual elements, not entire sets.
The Set.remove()
method returns true
if the Set
gets changed.
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);}}
Line 8: We declare a Set
of integer type: set1
. The object is created from the HashSet
class. This is because the Set
is an interface in Java and, hence, it cannot be instantiated.
Lines 9–13: We add the elements into the Set
using the Set.add()
method.
Line 15: We display the Set
before calling the remove()
method.
Lines 19–20: We call the remove()
function to remove specific elements from Set
, and assign the returned values to boolean
variables. When 150
passes as the argument, nothing happens. This is because it is not present. However, when 8
is removed, the change is reflected.
Line 21: We display the Set
after calling the remove()
method.
Lines 23–24: We display the returned values of the remove()
method.
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!
Haven’t found what you were looking for? Contact Us