Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over List<int[]> using Java 8 stream?

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.

like image 649
Karan Avatar asked Jun 28 '18 10:06

Karan


People also ask

How to iterate over list in Java using streams?

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 + " ");});

What is list iterator in Java?

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.

How to get an IntStream of array indexes in Java?

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.

How to iterate over a stream with the help of indices?

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


1 Answers

boolean noMatch = allNumList.stream()
    .flatMapToInt(Arrays::stream)
    .noneMatch(i -> i == numb);
like image 54
Tomasz Linkowski Avatar answered Oct 23 '22 05:10

Tomasz Linkowski