java8新特性——Stream

Stream流的一些灵活处理问题:

1.分组

2.list转map

3.过滤

代码举例:

        //准备名车集合
        List<Car> tempList = new ArrayList<>();
        Car Car5 = new Car("001", "奥迪", 1);
        Car Car1 = new Car("001", "奔驰", 2);
        Car Car2 = new Car("002", "宝马", 3);
        Car Car4 = new Car("002", "阿尔法", 4);
        Car Car3 = new Car("003", "兰博基尼", 5);
        tempList.add(Car1);
        tempList.add(Car2);
        tempList.add(Car3);
        tempList.add(Car4);
        tempList.add(Car5);

        //分组:根据车所属编号分组
        Map<String, List<Car>> map = tempList.stream().collect(Collectors.groupingBy(Car::getCarId));
        System.out.println(map);

/*           {001=[Car(carId=001, name=奔驰, price=2),
                Car(carId=001, name=奥迪, price=1)],
            002=[Car(carId=002, name=宝马, price=3),
                Car(carId=002, name=阿尔法, price=4)],
            003=[Car(carId=003, name=兰博基尼, price=5)]}*/


        //集合转化为map,如果特定字段相同的保留其中一个
        Map<String, Car> mapStr = tempList.stream().collect(Collectors.toMap(
                Car::getCarId, a -> a, (k1, k2) -> k1
        ));
        System.out.println(mapStr);

/*        {001=Car(carId=001, name=奔驰, price=2),
                002=Car(carId=002, name=宝马, price=3),
                003=Car(carId=003, name=兰博基尼, price=5)}*/


        //过滤筛选
        List<Car> filterList = tempList.stream().filter(m -> m.getName().equals("兰博基尼")).collect(Collectors.toList());
        System.out.println(filterList);

        /*[Car(carId=003, name=兰博基尼, price=5)]*/

 2021年07月01补充代码:

  private  Integer  studentNo;
    private  String  name;
    private  Integer  sex;

    public Integer getStudentNo() {
        return studentNo;
    }

    public void setStudentNo(Integer studentNo) {
        this.studentNo = studentNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentNo=" + studentNo +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                '}';
    } 
public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setStudentNo(1);
        stu1.setName("wlm");
        stu1.setSex(1);
        Student stu2 = new Student();
        stu2.setStudentNo(2);
        stu2.setName("wf");
        stu2.setSex(2);
        Student stu3 = new Student();
        stu3.setStudentNo(3);
        stu3.setName("zy");
        stu3.setSex(2);
        Student stu4 = new Student();
        stu4.setStudentNo(3);
        stu4.setName("zyl");
        stu4.setSex(1);
        List<Student> list = new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        List<Student> collect = list.stream().filter(item -> item.getSex() == 2 && "wf".equals(item.getName())).collect(Collectors.toList());
        System.out.println("过滤sex为2的wf学生:" + collect);
        Set<Integer> collect1 = list.stream().map(Student::getStudentNo).collect(Collectors.toSet());
        System.out.println("筛选获取所有的学号且去重:" + collect1);
        Map<Integer, String> collect2 = list.stream().collect(Collectors.toMap(Student::getStudentNo, Student::getName, (k1, k2) -> k2));
        System.out.println("转为map集合key为学号  名字为value---存在相同的k后者覆盖前者:" + collect2);
        Map<Integer, Student> collect3 = list.stream().collect(Collectors.toMap(Student::getStudentNo, student -> student, (k1, k2) -> k1));
        System.out.println("转为map集合key为学号  Student为value---存在相同的k取第一个,不覆盖:" + collect3.toString());
        HashMap<Object, Object> collect4 = list.stream().collect(HashMap::new, (m, p) -> m.put(p.getStudentNo(), p), HashMap::putAll);
        System.out.println("转为map集合key为学号  Student为value---key相同此方法默认覆盖前者:" + collect4.toString());
    }

  

posted @ 2021-04-13 16:15  Simoo23  阅读(54)  评论(0编辑  收藏  举报