jdk8 Stream List map 互转

List 转map

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author: haha
 * @date: 2019/3/27
 */
public class CollectorsDemo {

    /**
     * map 是映射,所有需要抽取属性的需要用到
     *
     * @param args
     */

    public static void main(String[] args) {
        User user1 = new CollectorsDemo().new User("张三", 14, 1,45.3d);
        User user2 = new CollectorsDemo().new User("里斯", 24, 1,52.3d);
        User user3 = new CollectorsDemo().new User("万五", 34, 0,47.2d);
        User user4 = new CollectorsDemo().new User("赵六", 44, 0,48.6d);
        User user5 = new CollectorsDemo().new User("陈启", 54, 1,56.7d);
        List userList = Arrays.asList(user1, user2, user3, user4, user5);

        //转化成map 以name为key,name 如果相同会报错,逻辑跟下面的分组是完全不一样的
        Map collect = userList.stream().collect(Collectors.toMap(User::getName, i -> i));

        //group by 分组
        Map> groupList = userList.stream().collect(Collectors.groupingBy(User::getGender));

        //从list种抽取出某一个属性
        List nameList = userList.stream().map(User::getName).collect(Collectors.toList());

        //map 转list
        List collect1 = groupList.entrySet().stream().map(e -> e.getKey()).collect(Collectors.toList());
//BigDecimal platAmount = platAllDateList.stream().filter(i -> //!Objects.equals(PaymentTradeTypeEnum.REFUND.getCode(), i.getRefundFlag()))
//                .map(i -> //i.getRefundAmount()).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        //累加 Collectors实现1
        Integer sumAge = userList.stream().filter(i -> i.getAge() > 0).collect(Collectors.summingInt(User::getAge));
        System.out.println("sumAge:" + sumAge);
        //累加 map实现2
        int sum = userList.stream().filter(i -> i.getAge() > 0).mapToInt(i -> i.getAge()).sum();
        System.out.println("sumAge2:" + sum);

        // 创建一个比较器,取名为 byHeight (通过高度来比较)
        Comparator byAge = Comparator.comparingInt(User::getAge);
        //统计每个性别中年龄最大的人
//        User{name='赵六', age=44, gender=0}
//        User{name='陈启', age=54, gender=1}
        Map> collect2 = userList.stream().collect(Collectors.groupingBy(User::getGender, Collectors.reducing(BinaryOperator.maxBy(byAge))));
        collect2.entrySet().forEach(i -> {
                 i.getValue().ifPresent(System.out::println);
        });

        // 定义映射 处理 四舍五入
        Function mapper = ps -> {
            Double weight = ps.getWeight();
            BigDecimal decimal = new BigDecimal(weight);
            Double d = decimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            ps.setWeight(d);
            return ps;
        };
    }
    

    class User {
        private String name;
        private int age;
        private int gender;
        private Double weight;

        public User(String name, int age, int gender,Double weight) {
            this.name = name;
            this.age = age;
            this.gender = gender;
            this.weight = weight;
        }

        public Double getWeight() {
            return weight;
        }

        public void setWeight(Double weight) {
            this.weight = weight;
        }

        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;
        }

        public int getGender() {
            return gender;
        }

        public void setGender(int gender) {
            this.gender = gender;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", gender=" + gender +
                    ", weight=" + weight +
                    '}';
        }
    }
}

你可能感兴趣的:(jdk8 Stream List map 互转)