Java ArrayList contains()

The contains() method checks if the specified element is present in the arraylist.

Example

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    // create an ArrayList
    ArrayList<String> languages = new ArrayList<>();
    languages.add("Java");
    languages.add("Python");
    languages.add("JavaScript");
    System.out.println("ArrayList: " + languages);

    // checks if 3 is present in the arraylist
System.out.print("Is Java present in the arraylist: "); System.out.println(languages.contains("Java"));
} } // Output: ArrayList: [Java, Python, JavaScript] // Is Java present in the arraylist: true

Syntax of ArrayList contains()

The syntax of the contains() method is:

arraylist.contains(Object obj)

Here, arraylist is an object of the ArrayList class.


contains() Parameter

The contains() method takes a single parameter.

  • obj - element that is checked if present in the arraylist

contains() Return Value

  • returns true if the specified element is present in the arraylist.
  • returns false if the specified element is not present in the arraylist.

Example 1: contains() Method with Integer ArrayList

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();

        // insert element to the arraylist
        numbers.add(2);
        numbers.add(3);
        numbers.add(5);
        System.out.println("Number ArrayList: " + numbers);

        // checks if 3 is present in the arraylist
        System.out.print("Is 3 present in the arraylist: ");
System.out.println(numbers.contains(3));
// checks if 1 is present in the arraylist System.out.print("Is 1 present in the arraylist: ");
System.out.println(numbers.contains(1));
} }

Output

Number ArrayList: [2, 3, 5]
Is 3 present in the arraylist: true
Is 1 present in the arraylist: false

In the above example, we have created an Integer arraylist named number. Notice the expressions,

// returns true
number.contains(3)

// returns false
number.contains(1)

Here, the contains() method checks if 3 is present in the list. Since 3 is present, the method returns true. However, 1 is not present in the list so the method returns false.


Example 2: contains() Method with String ArrayList

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // create an ArrayList
        ArrayList<String> languages = new ArrayList<>();

        // insert element to the arraylist
        languages.add("Java");
        languages.add("JavaScript");
        languages.add("Python");
        System.out.println("Programming Languages: " + languages);

        // checks if Java is present in languages
        System.out.print("Is Java present in the arraylist: ");
System.out.println(languages.contains("Java"));
// checks if C++ is present in languages System.out.print("Is C++ present in the arraylist: ");
System.out.println(languages.contains("C++"));
} }

Output

Programming Languages: [Java, JavaScript, Python]
Is Java present in the arraylist: true
Is C++ present in the arraylist: false

In the above example, we have used the contains() method to check if elements Java and C++ are present in the arraylist languages.

Since Java is present in the arraylist, the method returns true. However, C++ is not present in the list. Hence, the method returns false.

Note: The contains() method internally uses the equals() method to find the element. Hence, if the specified element matches with the element in arraylist, the method returns true.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges