我有以下课程:
public class Mark { private Long id; private Student student; private Integer value = 0; private Subject subject; } public enum Subject { MATH, CHEMISTRY }
我必须收到EnumMap
,其中值是来自的所有值的总和Mark
.
示例List
:
Mark(...,value = 1,subject = MATH)
Mark(...,value = 2,subject = MATH)
Mark(...,value = 5,subject = CHEMISTRY)
有了这个价值,我必须收到以下内容EnumMap
:
数学 - > 3
化学 - > 5
我认为应该这样做Collectors::groupingBy
,但我无法理解如何获得EnumMap
和他的价值.
markList.stream().collect( groupingBy( Mark::getSubject, () -> new EnumMap(Subject.class), summingInt(Mark::getValue)));