Skip to main content

How to Check if a List Contains a Value in Java with contains

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
boolean found = list.contains(value);
PartWhat it does
list.contains(value)Returns true if the list holds at least one element equal to value, and false if it doesn't.
valueThe value to look for, compared with each element's equals.
foundA 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 .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which creates a list of the roles customer and beta-tester with List.of, then prints whether it contains customer, admin and Customer with a capital C.

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​

Terminal
java Main.java
Output
true
false
false

A zsh terminal where java Main.java prints true, false and 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") is true, because one element equals customer.
  • roles.contains("admin") is false, because no element does.
  • roles.contains("Customer") is false. Two strings are equal only when every character is the same, and C isn't c.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting contains to ignore capital letterscontains("Customer") is false for a list that holds customerLook 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 valuePass a value of the list's element type: contains(500)
Looking for the text "500" in a list of numbersfalseLook for the number, not its text
contains(null) on a list made with List.ofNullPointerException, thrown from Objects.requireNonNull. An ArrayList returns false insteadCheck the value for null before you look for it

Troubleshooting​

SymptomLikely causeFix
contains returns false for a value you can see when you print the listCapital letters, spaces or the type differCompare the value with the printed elements
java.lang.NullPointerException at java.util.Objects.requireNonNull, with AbstractImmutableList.contains further down the stack tracecontains(null) on a list made with List.ofCheck 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.

Additional Resources​