Check if a value exists in an array in Java
This post will discuss how to check if a value exists in an array in Java. The solution should return true if the array contains the given element; false otherwise.
1. Using an intermediate List
You can use Arrays.asList() to get a list backed by the array and call the List::contains method to determine if a value is present in the list or not.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String[] values = {"B", "A", "C", "D", "E"}; String target = "A"; boolean found = Arrays.asList(values).contains(target); System.out.println(found); // true } } |
The above solution doesn’t work for arrays of primitives. If you prefer the Guava library, use the Ints.asList() method for primitives.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.primitives.Ints; public class Main { public static void main(String[] args) { int[] values = {4, 3, 6, 8, 5}; int target = 6; boolean found = Ints.asList(values).contains(target); System.out.println(found); // true } } |
2. Using an intermediate Set
Alternatively, you can construct a set from the array elements and call the Set::contains method to determine whether the given value is present in it. This is demonstrated below for a String array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Main { public static<T> boolean contains(T[] values, T target) { Set<T> set = new HashSet<>(Arrays.asList(values)); return set.contains(target); } public static void main(String[] args) { String[] values = {"B", "A", "C", "D", "E"}; String target = "A"; boolean found = contains(values, target); System.out.println(found); // true } } |
Here’s an example for primitive arrays using Java 8 Stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { int[] values = {5, 3, 4, 7, 6, 8}; int target = 4; boolean found = Arrays.stream(values) .boxed() .collect(Collectors.toSet()) .contains(target); System.out.println(found); // true } } |
3. Using Apache Commons Lang Library
One can also leverage the ArrayUtils.contains() method offered by Apache Commons Lang library, which returns true if the array contains the specified value. It is overloaded for all primitives types and object arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.apache.commons.lang3.ArrayUtils; public class Main { public static void main(String[] args) { String[] values = {"B", "A", "C", "D", "E"}; String target = "A"; boolean found = ArrayUtils.contains(values, target); System.out.println(found); // true } } |
4. Using Stream.anyMatch() method
With Java 8 and above, you can create a stream and check if any elements of the stream match with the given element using the anyMatch() method. This is demonstrated below for a String array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; public class Main { public static<T> boolean contains(T[] values, T target) { return Arrays.stream(values).anyMatch(target::equals); } public static void main(String[] args) { String[] values = {"B", "A", "C", "D", "E"}; String target = "A"; boolean found = contains(values, target); System.out.println(found); // true } } |
For primitive arrays, use the == operator to match with the given element.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] values = {5, 3, 2, 6, 3, 7}; int target = 2; boolean found = Arrays.stream(values).anyMatch(i -> i == target); System.out.println(found); // true } } |
5. Using Stream.filter() method
Another approach is to convert the given array into the stream and filter all occurrences of the specified element using the filter() method. Then, call the findFirst() method that returns an Optional.
|
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.Optional; public class Main { public static<T> boolean contains(T[] values, T target) { return Arrays.asList(values).stream() .filter(x -> x.equals(target)) .findFirst() .isPresent(); } public static void main(String[] args) { String[] values = {"B", "A", "C", "D", "E"}; String target = "A"; boolean found = contains(values, target); System.out.println(found); // true } } |
You can also call the count() method to get the count of the specified element in the array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; public class Main { public static<T> boolean contains(T[] values, T target) { return Arrays.asList(values).stream() .filter(x -> x.equals(target)) .count() > 0; } public static void main(String[] args) { String[] values = {"B", "A", "C", "D", "E"}; String target = "A"; boolean found = contains(values, target); System.out.println(found); // true } } |
For primitive arrays, use the == operator for comparison.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; public class Main { public static boolean contains(int[] values, int target) { return Arrays.stream(values) .filter(x -> x == target) .count() > 0; } public static void main(String[] args) { int[] values = {5, 3, 4, 7, 6, 8}; int target = 4; boolean found = contains(values, target); System.out.println(found); // true } } |
That’s all about checking if a value exists in an array 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 :)