stream流中的map传参的两种写法(同步解决方案)

package com.gang;

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

public class Test1 {
    public static void main(String[] args) {
        Student student1 = new Student("701","张三","da");
        Student student2 = new Student("700","李四","da");
        Student student3 = new Student("703","王五","da");
        Student student4 = new Student("701","赵六","da");
        Student student5 = new Student("700","钱七","da");
        Student student6 = new Student("701","老八","da");
        List<Student> list = Arrays.asList(student1, student2, student3);
        List<String> collect = list.stream().map(Student::getGrade).collect(Collectors.toList());
        List<String> collect1 = list.stream().map((stu) ->  {return stu.getGrade();}).collect(Collectors.toList());
        System.out.println(collect);
        System.out.println(collect1);

    }

}

package com.gang;

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

public class Test1 {
    public static void main(String[] args) {
        Student student = new Student("dsad1", "dada", "dada");
        Student student1 = new Student("dsad2", "dada", "dada");
        Student student11= new Student("dsad3", "dada", "dada");

        List<Student> list = Arrays.asList(student, student1, student11);
        //模拟for循环里面的集合
        List<Student> list2 = Arrays.asList(student, student1, student11);
        System.out.println(list2);
        Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getGrade));
        for (Map.Entry<String, List<Student>> entry : map.entrySet()) {
            //对应分组的id
            String key = entry.getKey();
            //创建分组 利用beanutils.copy复制对应的分组
            //这个list就是对应该分组下的人    //相当于外面的小集合等于说把一堆人分片了
            List<Student> value = entry.getValue();
            //meettype=2的在外面 3的是在里面  少的匹配多的
            for (Student student2 : value) {
                for (Student student3 : list2) {
                    //判断两个人的全局id是否相同如果相同则给里面的数据设置对应的分组id
                    if (student2.getGrade().equals(student3.getGrade())){
                        //这里配置人员和分组的关系
                        student3.setClassNumber(key);
                    }
                }

            }
        }
        System.out.println(list2);


    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值