, . :
List<String> xs = Arrays.asList(new String[]{ "one","two", "three"});
List<String> ys = Arrays.asList(new String[]{"four", "five"});
List<List<String>> input = Arrays.asList(xs, ys);
, Supplier:
Supplier<Stream<String>> result = input.stream()
.<Supplier<Stream<String>>>map(list -> list::stream)
, :
.reduce((sup1, sup2) -> () -> sup1.get().flatMap(e1 -> sup2.get().map(e2 -> e1 + e2)))
Reducing the return is optional, therefore, to process the missing value, I will return an empty stream of strings:
.orElse(() -> Stream.of(""));
In the end, we just need to get the provider value (which will be a stream of lines) and print it:
s.get().forEach(System.out::println);
The whole method will look like this:
public static void printCartesianProduct(List<String>... lists) {
List<List<String>> input = asList(lists);
Supplier<Stream<String>> s = input.stream()
.<Supplier<Stream<String>>>map(list -> list::stream)
.reduce((sup1, sup2) -> () -> sup1.get()
.flatMap(e1 -> sup2.get().map(e2 -> e1 + e2)))
.orElse(() -> Stream.of(""));
s.get().forEach(System.out::println);
}
source
share