What is the Set.remove() method in Java?

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].

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 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.

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 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.

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


What is `remove()` in Java?

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 `remove` method for a `Set`?

The Set.remove(Object obj) method removes the specified element from the Set and returns true if the Set is modified.


What is the `remove()` method of a `List` used for?

The remove() method in a List removes an element either by index or by matching an object.


How to use the `set()` method in Java?

The set(int index, E element) method in Java is used to replace the element at a specified index in a List, such as an ArrayList. It takes the index and the new element as arguments and returns the element previously at that position.


New on Educative
Learn any Language for FREE all September 🎉,
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources