Stream流中的groupingBy分组操作

Stream流中的groupingBy分组操作_第1张图片

 code:

//根据医院编号,查询医院所有科室信息 Department departmentQuery = new Department(); departmentQuery.setHoscode(hoscode); Example example = Example.of(departmentQuery); //所有科室列表 departmentList List departmentList = departmentRepository.findAll(example); //根据大科室编号 bigcode 分组,获取每个大科室里面下级子科室 Map> deparmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

2.

partitioningBy:该函数只能返回两个集合  一个是false,一个是true

Stream流中的groupingBy分组操作_第2张图片

Map> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));

3.

groupingBy:该函数返回每个分组下的所有集合数据
Map> group = personList.stream().collect(Collectors.groupingBy(Person::getSex));

Stream流中的groupingBy分组操作_第3张图片

4.附录所有代码 

Stream流中的groupingBy分组操作_第4张图片

 code:

package com.itheima.demo18_扩展字符串排序;

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

public class Harbor {
    public static void main(String[] args) {
        List personList = new ArrayList();
        personList.add(new Person("Tom", 8900, 28,"male", "New York"));
        personList.add(new Person("Jack", 7000, 29,"male", "Washington"));
        personList.add(new Person("Lily", 7800, 320,"female", "Washington"));
        personList.add(new Person("Anni", 8200, 26,"female", "New York"));
        personList.add(new Person("Owen", 9500, 27,"male", "New York"));
        personList.add(new Person("Alisa", 7900, 29,"female", "New York"));

        // 将员工按薪资是否高于8000分组
        Map> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
        // 将员工按性别分组
        Map> group = personList.stream().collect(Collectors.groupingBy(Person::getSex));
        List female = group.get("female");
        // 将员工先按性别分组,再按地区分组
        Map>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
        System.out.println("员工按薪资是否大于8000分组情况:" + part);
        System.out.println("员工按性别分组情况:" + group);
        System.out.println("员工按性别、地区:" + group2);



    }
}

6.javabean

package com.itheima.demo18_扩展字符串排序;

import java.util.Objects;

public class Person {
    private String name; // 姓名
    private int salary; // 薪资
    private int age; // 年龄
    private String sex; //性别
    private String area; // 地区

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return salary == person.salary &&
                age == person.age &&
                Objects.equals(name, person.name) &&
                Objects.equals(sex, person.sex) &&
                Objects.equals(area, person.area);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, salary, age, sex, area);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", area='" + area + '\'' +
                '}';
    }

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

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

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

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    // 构造方法
    public Person(String name, int salary, int age,String sex,String area) {
        this.name = name;
        this.salary = salary;
        this.age = age;
        this.sex = sex;
        this.area = area;
    }


}

 

你可能感兴趣的:(java,mysql,sql)