Java Set contains() containsAll() Methods Tutorial

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

What is Java Set contains() Method?

The Java Set contains() method is used to check and see if a Set object contains a specific value.

If it contained the specified element, the return value of this method becomes true, otherwise the value false will return instead.

Java Set contains() Method Syntax:

boolean contains(Object o)

contains() Method Parameters:

The method takes one argument and that is the element we want to search for in the target Set object.

contains() Method Return Value:

The return value of this method is of Boolean type.

If the target Set object contained the specified element, the return value of this method becomes true.

Otherwise, the value false will return instead.

Note: if the argument is null and the target Set object was empty as well, the return value of this method will be true as well.

contains() Method Exceptions:

The method does not throw an exception.

Example: using Set contains() 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.contains(400));
    }
}

Output:

true

What is Java Set containsAll() Method?

The Java Set containsAll() method is used to check a Set object for multiple elements and see if it has all those elements or not.

Basically, this method takes a reference to a collection and then checks every element there with the elements of the Set object. If all of those elements were in the Set object, then the return value of this method becomes true. Otherwise, even if one element of the reference collection wasn’t in the target Set method, the return value will be false.

Java Set containsAll() Method Syntax:

boolean containsAll(Collection<?> c)

containsAll() 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.

containsAll() Method Return Value:

The return value of this method is of boolean type.

If the target Set object had the entire elements of the reference collection, then the return value of this method becomes true.

Otherwise, the value false will return instead.

containsAll() Method Exceptions:

The method does not throw an exception.

Example: using Set containsAll() 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(100);
        array.add(200);
        array.add(300);
        boolean result = ts.containsAll(array);
        System.out.println(result);
    }
}

Output:

true

In this example, the list has three elements (100,200,300) and the Set object has these three elements as well. So when we’ve invoked the containsAll() method and passed a reference of the list as its argument, the final result became true.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies