In this section, we will learn what the List contains() and containsAll() methods are and how to use them in Java.
What is Java List contains() Method?
The Java List contains() method is used to search a list object to see if it contains a specific element.
For example, you have a list of 10,000 numbers and want to see if that list contains the number 5443. This method can be used to help you with.
Java List contains () Method Syntax:
boolean contains(Object o)
contains () Method Parameters:
The method takes one argument, and that is the value we want to look for in the target list object.
contains () Method Return Value:
The return value of this method is of type Boolean.
If the target list had the specified value (the one we set as the argument of the method) the return value will be true. Otherwise you’ll get the value false.
contains () Method Exceptions:
This method might throw two exceptions:
ClassCastException: we receive this exception if the value we set as the argument of the method is of a type that cannot be cast to the type of elements in the target list object.
NullPointerException: This exception will be thrown if we attempt to set the value `null` as the argument of this method.
Example: using List contains () method
import java.util.List; import java.util.ArrayList; public class Main { public static void main(String[] args){ List<String> list = new ArrayList<>(); list.add("One"); list.add("Two"); list.add("Three"); list.add("Four"); list.add("Five"); list.add("Six"); if (list.contains("Five")){ System.out.println("Yes, this list has the value five in it"); } } }
Output:
Yes, this list has the value five in it
In the list of the example above, there’s the value “Five”. Now we used the contains() method to check and see if this value exists in the list and because there’s one, the result of calling this method returned true and as a result the body of the if statement runs.
What is Java List containsAll() Method?
The Java List containsAll() method is used to check if the elements of a list object exist in the body of another list object.
Basically, this method is used to search for more than just one value in a list object!
Java List containsAll () Method Syntax:
boolean containsAll(Collection<?> c)
containsAll () Method Parameters:
The method takes one argument and that is the list object (or an object that implemented the Collection interface) that we want to search for its elements in the target list object (The one that the method is called with).
containsAll () Method Return Value:
The return value of this method is a Boolean.
If the entire elements of the list object we’ve set as the argument of this method is in the target list object (the main on that this method is called with) then the result of the method becomes true otherwise false.
containsAll () Method Exceptions:
This method throws the same exceptions that the contains() method might throw.
Example: using List containsAll () method
import java.util.List; import java.util.ArrayList; public class Main { public static void main(String[] args){ List<String> list = new ArrayList<>(); list.add("One"); list.add("Two"); list.add("Three"); list.add("Four"); list.add("Five"); list.add("Six"); List<String> secondList = new ArrayList<>(); secondList.add("Ond"); secondList.add("Two"); secondList.add("Ten"); if (list.containsAll(secondList)){ System.out.println("Yes, the entire elements of the reference list is in the first list as well"); }else{ System.out.println("No, some of the elements of the reference list is not in the main list"); } } }
Output:
No, some of the elements of the reference list is not in the main list
In this example, the second list has one value “ten” that is not in the first list and so for that reason the result of calling the containsAll() method was false.