To Check If a Value is Present in an Array in Java 8, we are using the Stream Api.
- Here Arrays.stream(arr) method convert Array into a Stream.
- Inside anyMatch(a -> a == value) method, it returns true if any elements in the stream match with given value.
- Print the Boolean status output.
import java.util.Arrays;
public class CheckValueStatus {
public static void main(String[] args) {
int arr[] = { 2, 4, 6, 8, 9 };
int value = 6;
boolean check = Arrays.stream(arr).anyMatch(a -> a == value);
System.out.println("This " + value + " is present in the array:- " + check);
}
}
Output :-
This 6 is present in the array:- true