Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use java 8 stream and lambda to flatMap a groupingBy result

I have a object with contains a list of other objects and I want to return a flatmap of the contained objects mapped by some property of the container. Any one if is it possible using stream and lambdas only?

public class Selling{
   String clientName;
   double total;
   List<Product> products;
}

public class Product{
   String name;
   String value;
}

Lets supose a list of operations:

List<Selling> operations = new ArrayList<>();

operations.stream()
     .filter(s -> s.getTotal > 10)
     .collect(groupingBy(Selling::getClientName, mapping(Selling::getProducts, toList());

The result would be of kind

Map<String, List<List<Product>>> 

but i would like to flatten it like

Map<String, List<Product>>
like image 279
Warley Noleto Avatar asked Aug 27 '15 14:08

Warley Noleto


2 Answers

You could try something like:

Map<String, List<Product>> res = operations.parallelStream().filter(s -> s.getTotal() > 10)
    .collect(groupingBy(Selling::getClientName, mapping(Selling::getProducts,
        Collector.of(ArrayList::new, List::addAll, (x, y) -> {
            x.addAll(y);
            return x;
        }))));
like image 128
Dennis Hunziker Avatar answered Oct 21 '22 01:10

Dennis Hunziker


In JDK9 there's new standard collector called flatMapping which can be implemented in the following way:

public static <T, U, A, R>
Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper,
                               Collector<? super U, A, R> downstream) {
    BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator();
    return Collector.of(downstream.supplier(),
            (r, t) -> {
                try (Stream<? extends U> result = mapper.apply(t)) {
                    if (result != null)
                        result.sequential().forEach(u -> downstreamAccumulator.accept(r, u));
                }
            },
            downstream.combiner(), downstream.finisher(),
            downstream.characteristics().toArray(new Collector.Characteristics[0]));
}

You can add it to your project and use like this:

operations.stream()
   .filter(s -> s.getTotal() > 10)
   .collect(groupingBy(Selling::getClientName, 
              flatMapping(s -> s.getProducts().stream(), toList())));
like image 43
Tagir Valeev Avatar answered Oct 21 '22 02:10

Tagir Valeev