Java Set remove() removeAll() Methods Tutorial

In this section, we will learn what the Set remove() and removeAll() methods are and how to use them in Java.

What is Java Set remove() Method?

The Java Set remove() method is used to remove an element from a Set object.

Java Set remove() Method Syntax:

boolean remove(Object o)

remove() Method Parameters:

The method takes one argument and that is the element we want to remove from the target Set object.

remove() Method Return Value:

The return value of this method is of type Boolean.

If the target Set object contained the specified element and removed it as a result of calling the method, then the return value of this method becomes true.

Otherwise, the value false will return instead.

remove() Method Exceptions:

The method does not throw an exception.

Example: using Set remove() method

import java.util.TreeSet; 
import java.util.Set;
 
class Main{
    public static void main(String[] args) {
        Set<Integer> ts = new TreeSet<>();
        ts.add(1);
        ts.add(2);
        ts.add(3);
        ts.add(0);
        ts.add(10);
        ts.add(30);
        ts.add(100);
        ts.add(200);
        ts.add(300);
        ts.add(400);
        ts.add(500);

        System.out.println(ts.remove(500));
        System.out.println(ts.remove(400));
        System.out.println(ts.remove(300));
    }
}

Output:

true

true

true

What is Java Set removeAll() Method?

The Java Set removeAll() method is used to remove more than one element from a Set object.

Basically, this method takes a reference to a collection object and will check the target Set object for every element in the reference collection. If it finds any element of the reference collection that was in the Set object as well, it will remove that from the Set object.

Java Set removeAll() Method Syntax:

boolean removeAll(Collection<?> c)

removeAll() Method Parameters:

The method takes one argument and that is a reference to a collection that we want to search its elements in a Set object.

removeAll() Method Return Value:

The return value of this method is of boolean type.

If as a result of calling this method, even one element was removed from the target Set object, then the return value of this method becomes true. Otherwise, the value false will return instead.

removeAll() Method Exceptions:

The method does not throw an exception.

Example: using Set removeAll() method

import java.util.TreeSet; 
import java.util.Set;
import java.util.ArrayList;
class Main{
    public static void main(String[] args) {
        Set<Integer> ts = new TreeSet<>();
        ts.add(1);
        ts.add(2);
        ts.add(3);
        ts.add(0);
        ts.add(10);
        ts.add(30);
        ts.add(100);
        ts.add(200);
        ts.add(300);
        ts.add(400);
        ts.add(500);

        ArrayList<Integer> array = new ArrayList<>();
        array.add(300);
        array.add(400);
        array.add(500);

        ts.removeAll(array);

        for (int i : ts){
            System.out.println(i);
        }
    }
}

Output:

0

1

2

3

10

30

100

200
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies