Flat-Mapping Collector for property of a Class using groupingBy(使用 groupingBy 的类属性的平面映射收集器)
问题描述
I have this class.
Then, I have a list of objects from that class.
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.
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
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
If you use that one, and also add a quantities
method to Assignment
that returns a Stream<Quantity>
, you can use this code:
这篇关于使用 groupingBy 的类属性的平面映射收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!