Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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 Set ageSet=newhashset(); Map Map=newhashmap(); 对于(整数年龄:年龄集){ 人=新人(年龄); 地图。放置(年龄、人); }_Java_Java Stream - Fatal编程技术网

如何在Java中使用流式处理在集合外创建HashMap Set ageSet=newhashset(); Map Map=newhashmap(); 对于(整数年龄:年龄集){ 人=新人(年龄); 地图。放置(年龄、人); }

如何在Java中使用流式处理在集合外创建HashMap Set ageSet=newhashset(); Map Map=newhashmap(); 对于(整数年龄:年龄集){ 人=新人(年龄); 地图。放置(年龄、人); },java,java-stream,Java,Java Stream,我正在尝试创建一个实例,并将其与其整数一起放入Hashmap中。如果我想使用parallelStream()(stream())对此进行编码,我如何才能做到这一点?我假设您的People类有一个getAge()函数 Set<Integer> ageSet = new HashSet<>(); Map<Integer, People> map = new HashMap<>(); for(int age : ageSet{ People p

我正在尝试创建一个实例,并将其与其整数一起放入Hashmap中。如果我想使用parallelStream()(stream())对此进行编码,我如何才能做到这一点?

我假设您的People类有一个getAge()函数

Set<Integer> ageSet = new HashSet<>();
Map<Integer, People> map = new HashMap<>();

for(int age : ageSet{
    People people = new People(age);
    map.put(age, people);
}
Map Map=ageSet.stream()
.地图(a->新人(a))
.collect(Collectors.toMap(People::getAge,p->p));

您可以通过usng收集流:

Map Map=ageSet.stream()
.collect(Collectors.toMap(Function.identity(),Person::new));

使用学生和id参考此示例:

Map<Integer, Person> map = ageSet.stream()
                                 .collect(Collectors.toMap(Function.identity(), Person::new));
//一组ID
Set idSet=new HashSet();
//填充一些ID
for(inti=0;i studentMap.put(id,newstudent(id));
//打印出来
System.out.println(学生地图);
Map<Integer, Person> map = ageSet.stream()
                                 .collect(Collectors.toMap(Function.identity(), Person::new));
    //set of ids
    Set<Integer> idSet = new HashSet<>();
    //populate some ids
    for(int i=0; i<5; i++)
    {
        idSet.add(i);
    }
    //create map
    Map<Integer, Student> studentMap = new HashMap<>();
    //stream id list to map with students
    idSet.stream().forEach(id -> studentMap.put(id, new Student(id)));
    //print out
    System.out.println(studentMap);