Check if a value exists in a List in Java
This post will discuss how to check if a value exists in a List in Java.
1. Using List.contains() method
To check whether a List contains a given element or not, you can simply use the List.contains() method as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int value = 3; boolean isExists = list.contains(value); System.out.println(isExists); // true } } |
You might want to override the equals() and hashCode() methods to make this work correctly with objects. Please note that the contains() method might end up iterating the complete list to find a value. For frequent calls, it’s better to convert the list into a set and call the contains() method on it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.HashSet; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int value = 3; boolean isExists = new HashSet<>(list).contains(value); System.out.println(isExists); // true } } |
2. Using Stream.filter() method
This can be easily done using Java 8 Stream filters. Here’s the code demonstrating the usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; import java.util.Objects; class Main { private static boolean contains(List<Integer> list, int value) { return list.stream() .filter(x -> Objects.equals(x, value)) .count() > 0; } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int value = 3; boolean isExists = contains(list, value); System.out.println(isExists); // true } } |
Here’s an even shorter version of the above code that makes use of the anyMatch() method to conditionally search an element in a List:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.Objects; class Main { private static boolean contains(List<Integer> list, int value) { return list.stream().anyMatch(e -> Objects.equals(e, value)); } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int value = 3; boolean isExists = contains(list, value); System.out.println(isExists); // true } } |
3. Using Apache Commons Collections
You can also use CollectionUtils.containsAny() provided by Apache Commons Collections that works in a similar way as Collections.contains() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.apache.commons.collections4.CollectionUtils; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int value = 3; boolean isExists = CollectionUtils.containsAny(list, value); System.out.println(isExists); // true } } |
4. Using enhanced for loop
If you use Java 7 or less, you can conditionally search for an element in the List using an enhanced for loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; import java.util.List; import java.util.Objects; class Main { private static boolean contains(List<Integer> list, int value) { for (int e : list) { if (Objects.equals(e, value)) { return true; } } return false; } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int value = 3; boolean isExists = contains(list, value); System.out.println(isExists); // true } } |
That’s all about checking if a value exists in a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)