EN
Java - check if ArrayList contains value
0
points
In this article, we would like to show you how to check if ArrayList contains a value in Java.
Quick solution:
myArrayList.contains(value);
Practical example
In this example, we check if letters
ArrayList contains an element with C
value using contains()
method that returns true or false.
import java.util.*;
public class Example {
public static void main(String[] args) {
List<String> letters = new ArrayList<>();
letters.add("A");
letters.add("B");
letters.add("C");
if (letters.contains("C")) {
System.out.println("The ArrayList contains C.");
} else {
System.out.println("The ArrayList doesn't contain C.");
}
}
}
Output:
The ArrayList contains C.