Given an array of elements of integers or long data type, you need to check if a given key is present in this array using pre defined functions in
java.util.stream.IntStream. The java.util.stream.IntStream/LongStream class contains a function anyMatch(), which helps to check if a particular element is present in an array.
Examples:
Input : arr[] = {1,2,3,4,5,6,7} , key = 3
Output : Yes
3 is present in the array.
Input : arr[] = {1,2,3,4,5,6,7} , key = 8
Output : No
The
Stream.anyMatch() method is used to check if the stream contains any such element which matches with a given predicate. It will return true if atleast 1 of the elements of the stream matches with the given predicate condition otherwise it will return false.
Syntax:
boolean anyMatch(Predicate< ? super T > predicate)
Below is a Java program on how to use anyMatch() method with both integer stream and stream of long integers.
Output:
Yes
Yes
Reference:
anyMatch() java docs