Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList contains() Method

Posted on July 22, 2025July 22, 2025 By Admin No Comments on Java ArrayList contains() Method

The contains() method is one of the most commonly used methods in the Java ArrayList class. It allows you to check whether a specific element exists in an ArrayList or not. This method is essential when you need to verify the presence of an item before performing operations such as adding, removing, or updating elements.

What Is the contains() Method in ArrayList?

  • The contains() method returns true if the specified element is present in the list.
  • It returns false if the element is not found.
  • Internally, it uses the equals() method to compare elements, so for custom objects, ensure the equals() method is properly overridden.
  • The method accepts an Object as a parameter and checks for its presence.

Syntax

public boolean contains(Object o)

Parameters

ParameterDescription
oThe element to be tested for presence in the list.

Return Value

  • Returns true if the ArrayList contains the specified element.
  • Returns false otherwise.

Exceptions

  • The method does not throw any checked exceptions.
  • If the element is null, it works correctly as it handles null elements internally.
  • If the list implementation does not support contains(), it may throw UnsupportedOperationException (rare for standard ArrayList).

How Does contains() Work Internally?

  • contains() iterates through all elements until it finds an element that is equal to the specified object (according to the equals() method).
  • If an element matches, it immediately returns true.
  • If it completes the iteration without a match, it returns false.
  • Because of this linear search, the time complexity is O(n) in the worst case, where n is the size of the list.

Examples of the contains() Method

1. Check Presence of a String in ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> cars = new ArrayList<>();
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");
        cars.add("Mazda");

        System.out.println(cars.contains("BMW"));     // true
        System.out.println(cars.contains("Toyota"));  // false
    }
}

Output:

true
false

2. Check Presence of an Integer in ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(2);
        numbers.add(4);
        numbers.add(6);
        numbers.add(8);

        System.out.println(numbers.contains(4));    // true
        System.out.println(numbers.contains(5));    // false
    }
}

3. Using contains() with Custom Objects

import java.util.ArrayList;

class Person {
    String name;
    Person(String name) {
        this.name = name;
    }

    // Override equals() for correct contains() behavior
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Person)) return false;
        Person other = (Person) obj;
        return this.name.equals(other.name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }
}

public class Demo {
    public static void main(String[] args) {
        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("Alice"));
        people.add(new Person("Bob"));

        System.out.println(people.contains(new Person("Alice")));  // true
        System.out.println(people.contains(new Person("Carol")));  // false
    }
}

Important Notes

  • The efficacy of contains() depends on the proper implementation of the equals() method for custom objects.
  • It performs a linear search, so avoid excessive use in very large lists for performance reasons.
  • Use collections optimized for faster searches like HashSet if frequent contains checks are needed.

Summary

AspectDetails
Method PurposeCheck if an element exists in an ArrayList
Syntaxboolean contains(Object o)
ParameterElement to find in the list
Returnstrue if found; false otherwise
ExceptionsNone
Time ComplexityO(n) — linear search

Conclusion

The contains() method in Java’s ArrayList is a simple yet powerful way to verify the presence of elements. Whether you are working with primitive wrapper types like String or Integer, or custom objects, contains() offers an easy, readable way to perform membership tests in a list. Always ensure your custom classes implement equals() correctly when using this method.

Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList clone() Method
Next Post: Java ArrayList ensureCapacity() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method

Recent Posts

  • Java ArrayList remove() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList iterator() Method
  • Java ArrayList isEmpty() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme