Creating a lambda function for iterating collections simultaneously

I am trying to use Java 8, I want to iterate over 2 collections and call a parameter function for each pair of values.

In the abstract I want to apply a function foo(tuple, i)for each iteration

[ v1, v2, v3, v4, v5, v6 ] (first collection)
[ w1, w2, w3, w4, w5, w6 ] (second collection)
---------------------------
  foo(<v1,w1>, 0)
  foo(<v2,w2>, 1)
  ...
  foo(<v6,w6>, 5)

Now what I got so far (java and pseudo code)

// Type of f?
private <S,U> void iterateSimultaneously(Collection<S> c1, Collection<U> c2, Function f) {
        int i = 0
        Iterator<S> it1 = c1.iterator()
        Iterator<U> it2 = c2.iterator()
        while(it1.hasNext() && it2.hasNext()) {
            Tuple<S, U> tuple = new Tuple<>(it1.next(), it2.next())             

            // call somehow f(tuple, i)

            i++
        }
}
 // ........................

// pseudo code, is this posible in Java?
iterateSimultaneously(c1, c2, (e1, e2, i) -> {
  // play with those items and the i value
})
+4
source share
4 answers

Something like this that you are looking for?

private <S,U> void iterateSimultaneously(Collection<S> c1, Collection<U> c2, BiConsumer<Tuple<S, U>, Integer> f) {
        int i = 0
        Iterator<S> it1 = c1.iterator()
        Iterator<U> it2 = c2.iterator()
        while(it1.hasNext() && it2.hasNext()) {
            Tuple<S, U> tuple = new Tuple<>(it1.next(), it2.next())             
            f.accept(tuple, i);
            i++
        }
}
iterateSimultaneously(c1, c2, (t, i) -> {
    //stuff
})

What type does the function f return? If nothing, replace it with the consumer. If you want him to take the tuple, you make it clear, as I did here. Is this what you are looking for?

+4
source

You may be looking for aBiConsumer :

private <S,U> void iterateSimultaneously(Collection<S> c1, Collection<U> c2,
                                         BiConsumer<Tuple<S, U>, Integer> f) {

  f.accept(tuple, i);
}

and name it with:

iterateSimultaneously(c1, c2, (tuple, i) -> doSomethingWith(tuple, i));

doSomethingWith :

private <S, U> void doSomethingWith(Tuple<S, U> tuple, int i) {

}
+4

Guava , Streams.zip Streams.mapWithIndex. :

Collection<Double> numbers = Arrays.asList(1.1, 2.2, 3.3, 4.4, 5.5);
Collection<String> letters = Arrays.asList("a", "b", "c", "d", "e");

Stream<Tuple<Double, String>> zipped = Streams.zip(
    numbers.stream(),
    letters.stream(),
    Tuple::new);

Stream<String> withIndex = Streams.mapWithIndex(
    zipped, 
    (tuple, index) -> index + ": " + tuple.u + "/" + tuple.v);

withIndex.forEach(System.out::println);

:

0: 1.1/a
1: 2.2/b
2: 3.3/c
3: 4.4/d
4: 5.5/e

zipping c1 c2 zipped, , , .


, Streams.mapWithIndex BiFunction, , . , , , , :

Stream<Tuple<Tuple<Double, String>, Long>> withIndex = Streams.mapWithIndex(
    zipped,
    Tuple::new);

withIndex.forEach(tuple -> someMethod(tuple.u, tuple.v));

someMethod :

<U, V> void method(Tuple<U, V> tuple, long index)

1: , Tuple:

public class Tuple<U, V> {
    private final U u;
    private final V v;

    Tuple(U u, V v) {
        this.u = u;
        this.v = v;
    }

    // TODO: getters and setters, hashCode and equals
}

2: , , .

3: Guava 21.0.

0

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


All Articles