0

我有以下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>> 格式。

我不知道如何申请gradeListmyList 流。

// 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, }, 
}

我可以听听你的意见吗?

4

1 回答 1

1

您可以链接两个groupingBy()收集器,但输出将不包含计数为 0 的等级:

Map<String,Map<String,Long>> grouped =
    myList.stream().collect(Collectors.groupingBy(
        MyClass::getLevel,
        Collectors.groupingBy(
            MyClass::getGrade,
            Collectors.counting())));

您可以使用第二条语句添加计数为 0 的等级:

grouped.forEach ((k,v)-> {gradeList.forEach (grade -> v.putIfAbsent (grade, 0L));});

grouped Map由第一条语句创建:

{Lv1={A=1}, Lv3={C=1, D=1}, Lv2={B=2}}

第二条grouped Map语句之后:

{Lv1={A=1, B=0, C=0, D=0, E=0}, Lv3={A=0, B=0, C=1, D=1, E=0}, Lv2={A=0, B=2, C=0, D=0, E=0}}
于 2020-12-02T08:57:05.757 回答