Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改我的语句以打印hashmap中对象成员类型的频率?_Java_Arraylist_Collections_Hashmap - Fatal编程技术网

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

Java 如何更改我的语句以打印hashmap中对象成员类型的频率?,java,arraylist,collections,hashmap,Java,Arraylist,Collections,Hashmap,我创建了一个包含各种属性的student对象的arrayList。在这些属性中有最喜欢的颜色。因此,在这个由n个学生组成的arraylist中,每个学生都有一个字符串favoriteColor成员变量 Set<student> studentUnique = new HashSet<student>(studentList); for (user key : studentUnique) { System.out.println(key + ": " + Coll

我创建了一个包含各种属性的student对象的arrayList。在这些属性中有最喜欢的颜色。因此,在这个由n个学生组成的arraylist中,每个学生都有一个字符串favoriteColor成员变量

Set<student> studentUnique = new HashSet<student>(studentList);
for (user key : studentUnique) {
    System.out.println(key + ": " + Collections.frequency(studentList, key));
}
我把我的学生列表(studentList)放到一个hashmap中,但我不知道如何编写频率语句来获得喜欢各自颜色的学生的频率

studentUnique.stream()
             .collect(Collectors.groupingBy(
                  Student::getColor, 
                  Collectors.counting()))
假设存在
getColor


假设存在
getColor

基本上,你所做的就是根据最喜欢的颜色进行“分组”

以下是另一种方法:

 Map<String, Integer> result = 
         studentList.stream()
                    .collect(toMap(Student::getFavouriteColor, s -> 1, Math::addExact));
所需进口:

import java.util.stream.*;
import static java.util.stream.Collectors.*;

基本上,你所做的就是根据你喜欢的颜色进行分组

以下是另一种方法:

 Map<String, Integer> result = 
         studentList.stream()
                    .collect(toMap(Student::getFavouriteColor, s -> 1, Math::addExact));
所需进口:

import java.util.stream.*;
import static java.util.stream.Collectors.*;

有没有一种方法可以让它提升?如蓝色=20,绿色=30,红色=50?@mobcitzkore按颜色升序?按频率升序(基本上是计数)@mobcitzkore实际上不在
地图中
如果你有
20=蓝色
20=红色
,你希望这如何成为一个
地图
,其中的键不能重复?可能是
20=[red,blue]
?我使用了aomine的forEachOrdered函数tidbit,非常感谢您的帮助有什么方法可以让它升序吗?如蓝色=20,绿色=30,红色=50?@mobcitzkore按颜色升序?按频率升序(基本上是计数)@mobcitzkore实际上不在
地图中
如果你有
20=蓝色
20=红色
,你希望这如何成为一个
地图
,其中的键不能重复?可能是
20=[red,blue]
?我使用了aomine的forEachOrdered函数tidbit,感谢您的帮助;参赛作品是如何印刷的?这是指单个学生对象吗?还是属性?@mobcityzkore这是一个打字错误,我现在已经编辑过了
e.getKey()
指的是我们按学生最喜欢的颜色分组的“东西”,而
e.getValue()
指的是出现的次数..forEachOrdered(e->System.out.println(entry+”:“+e.getValue());参赛作品是如何印刷的?这是指单个学生对象吗?还是属性?@mobcityzkore这是一个打字错误,我现在已经编辑过了
e.getKey()
表示我们根据学生最喜欢的颜色分组的“东西”,而
e.getValue()
表示出现的次数。