Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Flat-Mapping Collector for property of a Class using groupingBy

I have this class.

class Assignment {
  private Integer index;
  private List<Quantity> quantities;
}

Then, I have a list of objects from that class.

List<Assigment> assignments = new ArrayList<>();

Is there a way to create a Map that contains the index from Assignment and the List<Quantity> as values?

This is what I have tried so far.

assignments.stream().collect(groupingBy(Assignment::getIndex));

But this gives me a Map<Integer, List<Assignment>> and I want a Map<Integer, List<Quantity>>.

I have tried using forEach method - and it workes - but I'm sure there must be a way to do it in one liner - or at least using only collect and groupingBy methods

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It looks like there is no flat-mapping collector that you can use as a down-stream for groupingBy in Java8, but it has been proposed and accepted for Java9: https://bugs.openjdk.java.net/browse/JDK-8071600

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) -> mapper.apply(t).sequential().forEach(u -> downstreamAccumulator.accept(r, u)),
                            downstream.combiner(),
                            downstream.finisher(),
                            downstream.characteristics().stream().toArray(Collector.Characteristics[]::new));
    } 

If you use that one, and also add a quantities method to Assignment that returns a Stream<Quantity>, you can use this code:

Map<Integer, List<Quantity>> result = assignments.stream()
    .collect(groupingBy(Assignment::getIndex, 
        flatMapping(Assignment::quantities, toList())));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...