Java ArrayList.contains()

To check if an ArrayList contains specified element in Java, call contains() method on the given ArrayList and pass the element as argument to it.

In this tutorial, we will learn about the Java ArrayList.contains() method, and learn how to use this method to check if this ArrayList contains specified element, with the help of examples.

contains(Object o)

ArrayList.contains() returns true if this list contains the specified element/object. If the element is not present in this ArrayList, then contains() returns false.

ADVERTISEMENT

Syntax

The syntax of contains() method is

ArrayList.contains(Object obj)

where

ParameterDescription
objThe element whose presence in the ArraList has to be found.

Returns

The method returns boolean value.

Example 1 – contains(obj)

In this example, we will define a ArrayList of Strings and add some elements to it. The we shall check if element "c" is present in this ArrayList using contains() method. Since, the element is present in the HashSet, contains() method should return true.

Java Program

import java.util.Arrays;
import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {  
		ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList("a", "b", "c"));
		String element = "c";
		boolean result = arrayList.contains(element);
		System.out.println("ArrayList contains element \"" + element + "\" ?  " + result);
    }  
}

Output

ArrayList contains element "c" ?  true

Example 2 – contains(obj) – Element not present in ArrayList

In this example, we will define a ArrayList of Strings and add some elements to it. The we shall check if element "f" is present in this ArrayList using contains() method. Since, the element is not present in the ArrayList, contains() method should return false.

Java Program

import java.util.Arrays;
import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {  
		ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList("a", "b", "c"));
		String element = "f";
		boolean result = arrayList.contains(element);
		System.out.println("ArrayList contains element \"" + element + "\" ?  " + result);
    }  
}

Output

ArrayList contains element "f" ?  false

Example 3 – contains(obj) – ArrayList with User-defined Objects

In this example, we will define and initialize a ArrayList with objects of type Car. Then we will use contains() method to check if specific Car objects are present in this HashSet.

Java Program

import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {
		Car car1 = new Car(1, "Tesla");
		Car car2 = new Car(2, "BMW");
		Car car3 = new Car(3, "Toyota");
		
		ArrayList<Car> arrayList = new ArrayList<Car>();
		arrayList.add(car1);
		arrayList.add(car2);
		arrayList.add(car3);
		
		boolean result = arrayList.contains(car2);
		System.out.println("Is car2 present ?  " + result);
		
		result = arrayList.contains(new Car(2, "BMW"));
		System.out.println("Is this new Car object present ?  " + result);
    }  
}

class Car {
	int id;
	String name;
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
}

Output

Is car2 present ?  true
Is this new Car object present ?  false

In case of arrayList.contains(car2), we are passing the object instance for checking if it is present. As we have already added car2 to the ArrayList, contains() method returns true.

In case of arrayList.contains(new Car(2, "BMW")), we are passing a new object instance for checking if it is present. Since, we have not added this new object instance to ArrayList during initialization, contains() method returns false.

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.contains() method, and also learnt how to use this method with the help of Java example programs.