Java Lambda: Iterate over two dim arrays while keeping current index

I am new to Java 8 Lambda Expressions and want to formulate the following: I have a 2-dimensional array that I want to repeat several times in my application code and do things with the elements in the array. Before I do the following:

    public static abstract class BlaBlaIterator {

            private final BlaBla[][] blabla;

            public BlaBlaIterator(final BlaBla[][] blabla) {
                this.blabla = blabla;
            }

            public void iterate() {
                final int size = blabla.length;
                for (int x = 0; x < size; x++) {
                    for (int y = 0; y < size; y++) {
                        final BlaBla bla = blabla[x][y];
                        iterateAction(x, y, bla, bla == null);
                    }
                }
            }

            public abstract void iterateAction(int x, int y, BlaBla bla, boolean isNull);
        }

and then

    BlaBla[][] blabla = ...

    new BlaBlaIterator(blabla) {

        @Override
        public void iterateAction(final int x, final int y, final BlaBla bla, final boolean isNull) {
            //...
        }
    }.iterate();

The important thing: I need access to the current x / y, and I need to compute things like isNull.

Now I want to convert this to lambda. I want to write something like this:

    BlaBla[] blabla = ...
    blabla.stream().forEach((x, y, blabla, isNull) -> ... );

To get a stream from a two-dimensional array, I can do

    Arrays.stream(field).flatMap(x -> Arrays.stream(x))

But then I lose information about x / y and I can not transfer the calculated data, for example, isNull. How can i do this?

+4
source share
2 answers

, , - . "" Java-. , .

(Object-oriented). ArrayElement :

class ArrayElement<V> {
    public final int row;
    public final int col;
    public final V elem;
    ...
}

, (, flatMap), iterateAction

private static <T> Stream<ArrayElement<T>> createStream(int row, T[] arr) {
    OfInt columns = IntStream.range(0, arr.length).iterator();
    return Arrays.stream(arr).map(elem -> new ArrayElement<>(row, columns.nextInt(), elem));
} 

private static <V> void iterateAction(ArrayElement<V> elem) {
    System.out.println(elem);
}

, :

String[][] arr = {{"One", "Two"}, {"Three", "Four"}};
OfInt rows = IntStream.range(0, arr.length).iterator();
Arrays.stream(arr)
      .flatMap(subArr -> createStream(rows.nextInt(), subArr))
      .forEach(Main::iterateAction);

:

ArrayElement [row=0, col=0, elem=One]
ArrayElement [row=0, col=1, elem=Two]
ArrayElement [row=1, col=0, elem=Three]
ArrayElement [row=1, col=1, elem=Four]

, .

, , ArrayElement . , lamdba , ( ):

public class Main {    
    public static void main(String[] args) {
        String[][] arr = { {"One", "Two"}, {null, "Four"}};
        OfInt rows = IntStream.range(0, arr.length).iterator();
        Arrays.stream(arr).forEach(subArr -> iterate(subArr, rows.nextInt()));
    }
    static <T> void iterate(T[] arr, int row) {
        OfInt columns = IntStream.range(0, arr.length).iterator();
        Arrays.stream(arr).forEach(elem -> iterateAction(row, columns.nextInt(), elem, elem == null));
    }
    static <T> void iterateAction(int x, int y, T elem, boolean isNull) {
        System.out.println(x+", "+y+", "+elem+", "+isNull);
    }    
}

:

0, 0, One, false
0, 1, Two, false
1, 0, null, true
1, 1, Four, false

AtomicInteger

String[][] arr = {{"One", "Two"}, {null, "Four"}};
AtomicInteger rows = new AtomicInteger();
Arrays.stream(arr).forEach(subArr -> {
    int row = rows.getAndIncrement();
    AtomicInteger colums = new AtomicInteger();
    Arrays.stream(subArr).forEach(e -> iterateAction(row, colums.getAndIncrement(), e, e == null));
});

, .

Streams, , x y.

+7

, for -loop. , :

for(BlaBla[] array: blabla) for(BlaBla element: array) action(element);

, for-each, . Stream :

IntStream.range(0, blabla.length)
         .forEach(x -> IntStream.range(0, blabla[x].length)
             .forEach(y -> {
                       final BlaBla bla = blabla[x][y];
                       iterateAction(x, y, bla, bla == null);
             })
         );

1:1, , , Stream, , .

:

:

class ArrayElement {
    final int x, y;
    BlaBla element;
    final boolean isNull;
    ArrayElement(int x, int y, BlaBla obj) {
        this.x=x; this.y=y;
        element=obj; isNull=obj==null;
    }
}

:

IntStream.range(0, blabla.length).boxed()
         .flatMap(x -> IntStream.range(0, blabla[x].length)
                                .mapToObj(y->new ArrayElement(x, y, blabla[x][y])))
         .forEach(e -> iterateAction(e.x, e.y, e.element, e.isNull));
+1

Source: https://habr.com/ru/post/1569822/


All Articles