我有以下MyClass
清单
public class MyClass {
private String level;
private String grade;
private String value;
// constructor, getters, setters
}
List<MyClass> myList = new ArrayList<>();
myList.add(new MyClass("Lv1", "A", "value"));
myList.add(new MyClass("Lv2", "B", "value"));
myList.add(new MyClass("Lv2", "B", "value"));
myList.add(new MyClass("Lv3", "C", "value"));
myList.add(new MyClass("Lv3", "D", "value"));
还有一个grade list
List<String> gradeList = new ArrayList<>();
gradeList.add("A");
gradeList.add("B");
gradeList.add("C");
gradeList.add("D");
gradeList.add("E");
我想根据gradeList获得计数
作为Map<String, Map<String, Long>>
格式。
我不知道如何申请gradeList
myList 流。
// I guess
myList.stream().collect(Collectors.groupingBy(
MyClass::getLevel,
Collectors.groupingBy(
// do something with gradeList
, Collectors.counting())));
// expected result :
{
"Lv1"={"A"=1,"B"=0,"C"=0,"D"=0,"E"=0, },
"Lv2"={"A"=0,"B"=2,"C"=0,"D"=0,"E"=0, },
"Lv3"={"A"=0,"B"=0,"C"=1,"D"=1,"E"=0, },
}
我可以听听你的意见吗?