如何更改我的语句以在hashmap中打印对象成员类型的频率?

mob*_*ore 1 java collections arraylist hashmap

我创建了一个具有各种属性的student对象的arrayList.这些属性中最喜欢的颜色.所以在这个n学生的arraylist中,每个学生都有一个字符串favoriteColor成员变量.

Set<student> studentUnique = new HashSet<student>(studentList);
for (user key : studentUnique) {
    System.out.println(key + ": " + Collections.frequency(studentList, key));
}
Run Code Online (Sandbox Code Playgroud)

我想计算所说颜色的频率,例如有一百名学生,可能输出:

red: 50
blue: 20
green: 30
Run Code Online (Sandbox Code Playgroud)

我将学生的arrayList(studentList)放入一个hashmap,但我不知道如何编写我的频率语句来获得喜欢各自颜色的学生的频率.

Eug*_*ene 5

studentUnique.stream()
             .collect(Collectors.groupingBy(
                  Student::getColor, 
                  Collectors.counting()))
Run Code Online (Sandbox Code Playgroud)

假设getColor存在.