Java ProgramsCollectionsArrayList Search

ArrayList Search in Java

beginner·  Collections  ·  List

Problem

ArrayList offers two direct ways to search: contains(), which just answers yes-or-no, and indexOf(), which also reports where a match was found.

Given an ArrayList of fruits, check whether one value is present and find the position of another, plus confirm a missing value reports as absent.

Input
add("Apple"), add("Banana"), add("Cherry")
Output
Contains Banana: true

Java Program

Java
import java.util.ArrayList; public class ArrayListSearch { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); System.out.println("Contains Banana: " + fruits.contains("Banana")); System.out.println("Index of Cherry: " + fruits.indexOf("Cherry")); System.out.println("Index of Mango: " + fruits.indexOf("Mango")); } }

Output

Contains Banana: true Index of Cherry: 2 Index of Mango: -1

Core Logic

Both contains() and indexOf() scan the list from the start looking for a match — contains() stops at the first true/false answer, while indexOf() also reports exactly where that match was.

How It Works
  1. 1contains("Banana") scans the list until it finds an equal element, returning true as soon as one matches.
  2. 2indexOf("Cherry") scans the same way but returns the matching position instead of a boolean — 2 here, since Cherry is the third element.
  3. 3indexOf("Mango") scans the whole list without finding a match, so it returns -1 — the standard 'not found' sentinel.
  4. 4Both methods rely on equals() to decide what counts as a match, the same comparison String uses for content equality.
Cherry sits at index 2 in [Apple, Banana, Cherry], so indexOf("Cherry") returns 2; Mango isn't in the list at all, so its index comes back -1.
💡

Key Point: -1 is a real, meaningful return value here — checking indexOf(x) != -1 is a common way to test presence when the position might also be needed, instead of calling contains() separately.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: ArrayList keeps no auxiliary index for its contents, so both contains() and indexOf() must scan up to all n elements in the worst case before confirming a match or exhausting the list.

Key Concepts

contains()indexOf()linear search

Approach 2: Java 8

Java
import java.util.ArrayList; public class ArrayListSearchStream { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); // Short-circuits at the first matching element, same as contains() boolean hasBanana = fruits.stream().anyMatch(f -> f.equals("Banana")); System.out.println("Contains Banana: " + hasBanana); } }

Output

Contains Banana: true

Core Logic

anyMatch() expresses the same 'does a matching element exist' question as a stream predicate, without naming contains() directly.

How It Works
  1. 1fruits.stream() opens a stream over the list's elements.
  2. 2.anyMatch(f -> f.equals("Banana")) checks each element against the given condition, short-circuiting as soon as one matches.
  3. 3The result is the same boolean contains() would produce for an equality check, just expressed as an arbitrary predicate instead of a fixed value.
  4. 4Unlike contains(), this same technique could just as easily test a more complex condition than plain equality, since anyMatch() takes any Predicate.
Streaming [Apple, Banana, Cherry] and checking f.equals("Banana") matches on the second element, so anyMatch() returns true.
💡

Key Point: anyMatch() short-circuits the same way contains() does — it stops scanning the moment a match is found, rather than checking every remaining element.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: anyMatch() still scans up to n elements in the worst case and stops at the first match, the same cost as contains(), just expressed through a predicate instead of a fixed equality check.

Key Concepts

StreamanyMatch()

Related Programs