根据对象某个属性来分组
例如根据地区 性别 区分集合
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, "male", "New York"));
personList.add(new Person("Jack", 7000, "male", "Washington"));
personList.add(new Person("Lily", 7800, "female", "Washington"));
personList.add(new Person("Anni", 8200, "female", "New York"));
personList.add(new Person("Owen", 9500, "male", "New York"));
personList.add(new Person("Alisa", 7800, "female", "New York"));
//根据性别分组 没有判断条件
Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(Person::getSex));
//根据薪资 分组 却分开
Map<Boolean, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(person -> person.getSalary() > 8000));
//根据性别 统计个数
Map<String,Long> map = personList.stream().map(Person::getSex)
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting())//传什么 返回什么
);
//根据工资是否大于8000 分组 映射表
Map<Boolean, List<Person>> collect2 = personList.stream()
.collect(Collectors.groupingBy(person -> person.getSalary() > 8000, Collectors.toList()));
// 根据工资去归纳每一个
Map<Integer, List<Person>> collect3 = personList.stream()
.collect(Collectors.groupingBy(Person::getSalary, Collectors.toList()));
// 不达标的具体属性 区分
Map<Boolean, List<Integer>> collect4 = personList.stream().map(Person::getSalary).collect(Collectors.groupingBy(x -> x > 8000, Collectors.toList()));
// 根据确定属性 区分
Map<Integer, List<Integer>> collect5 = personList.stream().map(Person::getSalary).collect(Collectors.groupingBy(Function.identity(), Collectors.toList()));
System.out.println(collect);
System.out.println(collect1);
System.out.println(collect2);
System.out.println(collect3);
System.out.println(collect4);
System.out.println(collect5);
System.out.println(map);
结果
{female=[Person(name=Lily, salary=7800, age=0, sex=female, area=Washington), Person(name=Anni, salary=8200, age=0, sex=female, area=New York), Person(name=Alisa, salary=7800, age=0, sex=female, area=New York)], male=[Person(name=Tom, salary=8900, age=0, sex=male, area=New York), Person(name=Jack, salary=7000, age=0, sex=male, area=Washington), Person(name=Owen, salary=9500, age=0, sex=male, area=New York)]}
{false=[Person(name=Jack, salary=7000, age=0, sex=male, area=Washington), Person(name=Lily, salary=7800, age=0, sex=female, area=Washington), Person(name=Alisa, salary=7800, age=0, sex=female, area=New York)], true=[Person(name=Tom, salary=8900, age=0, sex=male, area=New York), Person(name=Anni, salary=8200, age=0, sex=female, area=New York), Person(name=Owen, salary=9500, age=0, sex=male, area=New York)]}
{false=[Person(name=Jack, salary=7000, age=0, sex=male, area=Washington), Person(name=Lily, salary=7800, age=0, sex=female, area=Washington), Person(name=Alisa, salary=7800, age=0, sex=female, area=New York)], true=[Person(name=Tom, salary=8900, age=0, sex=male, area=New York), Person(name=Anni, salary=8200, age=0, sex=female, area=New York), Person(name=Owen, salary=9500, age=0, sex=male, area=New York)]}
{8900=[Person(name=Tom, salary=8900, age=0, sex=male, area=New York)], 8200=[Person(name=Anni, salary=8200, age=0, sex=female, area=New York)], 7800=[Person(name=Lily, salary=7800, age=0, sex=female, area=Washington), Person(name=Alisa, salary=7800, age=0, sex=female, area=New York)], 7000=[Person(name=Jack, salary=7000, age=0, sex=male, area=Washington)], 9500=[Person(name=Owen, salary=9500, age=0, sex=male, area=New York)]}
{false=[7000, 7800, 7800], true=[8900, 8200, 9500]}
{8900=[8900], 8200=[8200], 7800=[7800, 7800], 7000=[7000], 9500=[9500]}
{female=3, male=3}