根据对象属性分组区分

根据对象某个属性来分组
例如根据地区 性别 区分集合

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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值