ArrayList Search in Java
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.
Java Program
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
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.
- 1
contains("Banana")scans the list until it finds an equal element, returningtrueas soon as one matches. - 2
indexOf("Cherry")scans the same way but returns the matching position instead of a boolean —2here, sinceCherryis the third element. - 3
indexOf("Mango")scans the whole list without finding a match, so it returns-1— the standard 'not found' sentinel. - 4Both methods rely on
equals()to decide what counts as a match, the same comparisonStringuses 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.
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
Approach 2: Java 8
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
Core Logic
anyMatch() expresses the same 'does a matching element exist' question as a stream predicate, without naming contains() directly.
- 1
fruits.stream()opens a stream over the list's elements. - 2
.anyMatch(f -> f.equals("Banana"))checks each element against the given condition, short-circuiting as soon as one matches. - 3The result is the same boolean
contains()would produce for an equality check, just expressed as an arbitrary predicate instead of a fixed value. - 4Unlike
contains(), this same technique could just as easily test a more complex condition than plain equality, sinceanyMatch()takes anyPredicate.
[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.
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.