Ad space (banner)
โ˜•Java Lessons
Lesson 22 / 68

Searching a Collection (contains)

This lesson covers searching a collection in Java with List, so you can check whether a target value is present. It's for anyone searching "Java List contains usage."

.contains(value) checks whether a value is present in a list. You can use this search method even on an immutable list created with List.of().

The example creates a list with List.of("apple", "orange", "grape"), and checks whether "orange" is present with fruits.contains("orange"). Unlike an array, a list comes with handy search methods like this built right in.

A common early mistake is not seeing the difference between an array ([]) and a list (List). An array has no .contains() method โ€” you'd need to convert it to a list, or write your own loop.

In real projects, this search is used constantly to check whether a specific product ID or username is present in a list.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)