How to Check if a List Contains a Value in Java with contains
list.contains(value) returns true when a Java list holds an element equal to value, and false when it doesn't. It compares with equals, so a string has to match exactly, capital letters included, and a number has to have the same type.
Syntax
boolean found = list.contains(value);
| Part | What it does |
|---|---|
list.contains(value) | Returns true if the list holds at least one element equal to value, and false if it doesn't. |
value | The value to look for, compared with each element's equals. |
found | A boolean that holds the answer. |
The List documentation: "Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e)."
The Example
The example makes a list of two roles, customer and beta-tester, and checks it for customer, for admin and for Customer with a capital C.
Prerequisites
- A JDK 11 or later, which can run a single
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> roles = List.of("customer", "beta-tester");
System.out.println(roles.contains("customer"));
System.out.println(roles.contains("admin"));
System.out.println(roles.contains("Customer"));
}
}
Step 2: Run It
java Main.java
true
false
false

java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.
What Happened
roles.contains("customer")istrue, because one element equalscustomer.roles.contains("admin")isfalse, because no element does.roles.contains("Customer")isfalse. Two strings are equal only when every character is the same, andCisn'tc.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting contains to ignore capital letters | contains("Customer") is false for a list that holds customer | Look for the exact text |
Looking for a number of another type, as in contains(500L) in a List<Integer> | It compiles, and returns false: a Long never equals an Integer, even with the same value | Pass a value of the list's element type: contains(500) |
Looking for the text "500" in a list of numbers | false | Look for the number, not its text |
contains(null) on a list made with List.of | NullPointerException, thrown from Objects.requireNonNull. An ArrayList returns false instead | Check the value for null before you look for it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
contains returns false for a value you can see when you print the list | Capital letters, spaces or the type differ | Compare the value with the printed elements |
java.lang.NullPointerException at java.util.Objects.requireNonNull, with AbstractImmutableList.contains further down the stack trace | contains(null) on a list made with List.of | Check for null first |
FAQ
How do I find where a value is in a list?
Use indexOf, which "Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element." On the list from this example, roles.indexOf("beta-tester") returned 1, and roles.indexOf("admin") returned -1.
Why does contains(500L) compile on a List of Integer?
contains takes any Object, as its signature contains(Object o) shows. The compiler lets any value through, and the answer is false when no element equals it.
Conclusion
list.contains(value) answers whether a list holds a value, comparing with equals. Look for the exact text, use the list's own element type for numbers, and keep null away from lists made with List.of.
