个人常用stream流处理数据

People实体类

public class People {

    private int id;

    private String name;

    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

stream流例子

public class Test1 {

    public static void main(String[] args) {
        List<People> peopleList = new ArrayList<>();

        People p1 = new People();
        p1.setId(1);
        p1.setName("Tom");
        p1.setAge(33);
        peopleList.add(p1);

        People p2 = new People();
        p2.setId(2);
        p2.setName("Jim");
        p2.setAge(18);
        peopleList.add(p2);

        People p3 = new People();
        p3.setId(3);
        p3.setName("Ken");
        p3.setAge(19);
        peopleList.add(p3);

        //将peopleList中所有people的id组合成一个list
        List<Integer> peopleIds = peopleList.stream().map(People::getId).collect(Collectors.toList());

        List<Integer> deletedPeopleIds = new ArrayList<>();
        deletedPeopleIds.add(1);
        deletedPeopleIds.add(2);

        //peopleList删除id为[1,2]的people
        List<People> deletedPeopleList = peopleList.stream().filter(p -> !deletedPeopleIds.contains(p.getId())).collect(Collectors.toList());

        //peopleList按age升序排序
        List<People> peopleListSortAsc = peopleList.stream().sorted(Comparator.comparing(People::getAge)).collect(Collectors.toList());

        //peopleList按age降序排序
        List<People> peopleListSortDesc = peopleList.stream().sorted(Comparator.comparing(People::getAge).reversed()).collect(Collectors.toList());

        //key为id,value为people,建peopleMap(注意id作为key,要求不重复)
        Map<Integer, People> peopleMap = peopleList.stream().collect(Collectors.toMap(People::getId, Function.identity()));

        List<People> peopleList1 = new ArrayList<>();

        People p4 = new People();
        p4.setId(1);
        p4.setName("小黄");
        p4.setAge(11);

        People p5 = new People();
        p5.setId(2);
        p5.setName("小白");
        p5.setAge(13);

        People p6 = new People();
        p6.setId(1);
        p6.setName("小紫");
        p6.setAge(19);

        peopleList1.add(p4);
        peopleList1.add(p5);
        peopleList1.add(p6);

        //peopleList1以People的id为key,People为value组合成map,如果存在key(id)重复,取age最大的People为value
        Map<Integer, People> peopleMap1 = peopleList1.stream().collect(Collectors.toMap(People::getId, Function.identity(), (oldValue, newValue) -> {
            if (oldValue.getAge() < newValue.getAge()) {
                return newValue;
            } else {
                return oldValue;
            }
        }));

		List<People> peopleList2 = new ArrayList<>();

        peopleList2.add(p1);
        peopleList2.add(p2);
        peopleList2.add(p3);
        peopleList2.add(p6);

        //peopleList2按People的age进行分组
        Map<Integer, List<People>> peopleMap2 = peopleList2.stream().collect(Collectors.groupingBy(p -> p.getAge()));
    }


}

优雅强大,nice!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值