I have
List<int[]> allNumList = new ArrayList<>();
I am iterating over allNumList
and matching one condition in below code .
for (int[] arr : allNumList) {
for (int i : arr) {
if (i == numb) {
return false;
}
}
}
I want to do above code using Java 8.
You can use stream () method of the List interface which gives a stream to iterate using forEach method. In forEach method, we can use the lambda expression to iterate over all elements. The following code snippet shows the usage of streams to iterate over the list. list.stream ().forEach (i -> {System.out.print (i + " ");});
ListIterator is an iterator is a java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object. It is used to iterator over a list using while loop.
The idea is to get an IntStream of array indices, ranging from 0 to n-1, where n is the array’s length. Then we use the mapToObj (mapper) method to returns a stream of string, as shown below: 2. Using AtomicInteger We can also use AtomicInteger class in Java.
Given a Stream in Java, the task is to iterate over it with the help of indices. Recommended: Please try your approach on {IDE} first, before moving on to the solution. Method 1: Using IntStream. Get the Stream from the array using range () method. Map each elements of the stream with an index associated with it using mapToObj () method
boolean noMatch = allNumList.stream()
.flatMapToInt(Arrays::stream)
.noneMatch(i -> i == numb);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With