java8的新特性,快速实现List转Map,分组,过滤

1.创建实体

public class Apple {
     
        private Integer id;
        private String name;
        private BigDecimal money;
        private Integer num;

    public Integer getId() {
     
        return id;
    }

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

    public String getName() {
     
        return name;
    }

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

    public BigDecimal getMoney() {
     
        return money;
    }

    public void setMoney(BigDecimal money) {
     
        this.money = money;
    }

    public Integer getNum() {
     
        return num;
    }

    public void setNum(Integer num) {
     
        this.num = num;
    }

    @Override
    public String toString() {
     
        return "Apple{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                ", num=" + num +
                '}';
    }

    public Apple(){
     }

    public Apple(Integer id, String name, BigDecimal money, Integer num) {
     
            this.id = id;
            this.name = name;
            this.money = money;
            this.num = num;
        }
}

2.给List赋值

List<Apple> appleList = new ArrayList<>();//存放apple对象集合

        Apple apple11 =  new Apple(1,"苹果1",new BigDecimal("3.25"),10);
        Apple apple12 = new Apple(1,"苹果2",new BigDecimal("1.35"),20);
        Apple applel3 =  new Apple(2,"香蕉",new BigDecimal("2.89"),30);
        Apple applel4 =  new Apple(3,"荔枝",new BigDecimal("9.99"),40);

        appleList.add(apple11);
        appleList.add(apple12);
        appleList.add(applel3);
        appleList.add(applel4);

3.根据ID进行分组

        //List 以ID分组 Map>
        Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));

        System.out.println("groupBy:" + groupBy);

4.List -> Map

/**
         * List -> Map
         * toMap 如果集合对象有重复的key,会报错Duplicate key ....
         * apple1,apple12的id都为1。
         * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
         */
        Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));

5.Map的三种遍历方式

        //map的遍历第一种,二次取值(常用)
        Set<Integer> integers = appleMap.keySet();
        for (Integer integer : integers) {
     
            System.out.println("第一种->" + appleMap.get(integer));
        }
		//map的遍历第二种,迭代器
        Iterator<Map.Entry<Integer, Apple>> iterator = appleMap.entrySet().iterator();
        while (iterator.hasNext()){
     
            Map.Entry<Integer, Apple> next = iterator.next();
            System.out.println("第二种->" + next.getKey() + ":" + next.getValue());
        }
//map的遍历第三种,适合大容量遍历
        for (Map.Entry<Integer, Apple> integerAppleEntry : appleMap.entrySet()) {
     
            System.out.println("第三种->" + integerAppleEntry.getKey() + ":" + integerAppleEntry.getValue());
        }
        //.values()不赘述

6.Filter

//过滤出符合条件的数据
        List<Apple> filterList = appleList.stream().filter(a -> a.getName().equals("香蕉")).collect(Collectors.toList());
        System.out.println("filterList:"+filterList);

7.BigDecimal的统计

        //计算 总金额
        BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("totalMoney:"+totalMoney);  

8.去重

        // 根据id去重
        List<Apple> collect = appleList.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(Apple::getId))), ArrayList::new));

        //遍历集合
        collect.stream().forEach(ret ->{
     
            System.out.println(ret);
        });

人若成就一道景致,则无关春夏秋冬。

你可能感兴趣的:(简洁开发,java,lambda)