Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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>>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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())));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...