Stream流中的groupingBy分组操作
短信预约 -IT技能 免费直播动态提醒
code:
//根据医院编号,查询医院所有科室信息 Department departmentQuery = new Department(); departmentQuery.setHoscode(hoscode); Example example = Example.of(departmentQuery); //所有科室列表 departmentList List
partitioningBy:该函数只能返回两个集合 一个是false,一个是true
Map> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
groupingBy:该函数返回每个分组下的所有集合数据
Map> group = personList.stream().collect(Collectors.groupingBy(Person::getSex));
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; }}
来源地址:https://blog.csdn.net/weixin_61503139/article/details/126074858
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341