Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace nested loop with Java 8 flatmap

I'm trying to use flatmap to make a nested loop with the Stream API, but I can't seem to figure it out. As an example, I want to recreate the following loop:

List<String> xs = Arrays.asList(new String[]{ "one","two", "three"});
List<String> ys = Arrays.asList(new String[]{"four", "five"});

System.out.println("*** Nested Loop ***");
for (String x : xs)
    for (String y : ys)
        System.out.println(x + " + " + y);

I can do it like this, but this seems ugly:

System.out.println("*** Nested Stream ***");
xs.stream().forEach(x ->
    ys.stream().forEach(y -> System.out.println(x + " + " + y))
);

Flatmap looks promising, but how can I access the variable in the outer loop?

System.out.println("*** Flatmap *** ");
xs.stream().flatMap(x -> ys.stream()).forEach(y -> System.out.println("? + " + y));

Output:

*** Nested Loop ***
one + four
one + five
two + four
two + five
three + four
three + five
*** Nested Stream ***
one + four
one + five
two + four
two + five
three + four
three + five
*** Flatmap *** 
? + four
? + five
? + four
? + five
? + four
? + five
like image 204
wvdz Avatar asked Nov 18 '16 13:11

wvdz


2 Answers

You have to create your desired elements in the flatMap stage, like:

xs.stream().flatMap(x -> ys.stream().map(y -> x + " + " + y)).forEach(System.out::println);
like image 131
Flown Avatar answered Sep 19 '22 19:09

Flown


Normally, there is no need of the flatMap:

xs.forEach(x -> ys.stream().map(y ->  x + " + " + y).forEach(System.out::println)); // X
xs.forEach(x -> ys.forEach(y -> System.out.println(x + " + " + y))); // V

as well as there is no need of Stream API here.

Yes, it looks beautiful, but only with such childish tasks. You create/close a new stream for each element only to merge them into the resulting stream. And all of that is just for printing out?

In contrast, the forEach provides a one-line solution without any performance costs (a standard foreach inside).

like image 34
Andrew Tobilko Avatar answered Sep 18 '22 19:09

Andrew Tobilko