Java TreeSet - remove() Method
The java.util.TreeSet.remove() method is used to remove the specified element from the set, if it is present. Every removal of element results into reducing the set size by one unless the set is empty.
Syntax
public boolean remove(Object obj)
Parameters
obj |
Specify the object to be removed from this set, if present. |
Return Value
Returns true if this set contained the specified element.
Exception
NA.
Example:
In the example below, the java.util.TreeSet.remove() method is used to remove the specified element from the given set.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating treeset TreeSet<Integer> MySet = new TreeSet<Integer>(); //populating the set MySet.add(10); MySet.add(20); MySet.add(30); MySet.add(40); //printing the set System.out.println("MySet contains: " + MySet); //remove 30 from the set MySet.remove(30); //printing the set System.out.println("MySet contains: " + MySet); } }
The output of the above code will be:
MySet contains: [10, 20, 30, 40] MySet contains: [10, 20, 40]
❮ Java.util - TreeSet