java 8 stream API

java 8 stream API

  • 1 Collectors
    • 1.1 groupingBy
    • 1.2 joining
    • 1.3 partitioningBy
    • 1.4 toMap
    • 1.5 averagingDouble
    • 1.6 maxBy | minBy
    • 1.7 summingLong
    • 1.8 summarizingLong
  • 2. 流操作
    • 2.1 SummaryStatistics
    • 2.2. reduce

1 Collectors

  • 操控的实体类
   @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    private static class User{
        private Long id;
        private String name;
        private Integer age;
        private Boolean isValid;
        @JSONField(format = "yyyy-MM-dd HH:mm:ss")
        private Date birth;
    }
  • 已经存在的用户集合
User u1 = User.builder().id(1L).name("zs").age(18).isValid(true).birth(new Date()).build();
User u2 = User.builder().id(2L).name("zs xxx").age(18).isValid(true).birth(new Date()).build();
User u3 = User.builder().id(3L).name("xxx 001").age(20).isValid(true).birth(new Date()).build();
User u4 = User.builder().id(4L).name("zs xxx 001").age(24).isValid(true).birth(new Date()).build();
User u5 = User.builder().id(5L).name("001 zs xxx").age(24).isValid(true).birth(new Date()).build();
List<User> userList = Arrays.asList(u1, u2, u3, u4, u5);

1.1 groupingBy

  • groupingBy 通过对象某个字段进行 分组
Map<Integer, List<User>> collect_1 = userList.stream().collect(Collectors.groupingBy(User::getAge));

1.2 joining

  • 对结果集进行 连接
String collect_2 = userList.stream().map(User::getName).collect(Collectors.joining(",","[","]"));

1.3 partitioningBy

  • 通过自定义的一个条件 进行分组
Map<Boolean, List<User>> collect_3 = userList.stream().collect(Collectors.partitioningBy(user -> user.getAge() != null && user.getAge() > 20));

1.4 toMap

  • List 转换成 Map
Map<String, User> stringUserMap = userList.stream().collect(Collectors.toMap(User::getName, user -> user));

1.5 averagingDouble

  • 对所有用户集合的age进行求平均值
Double collect1 = userList.stream().collect(Collectors.averagingDouble(value -> value.getAge().doubleValue()));

1.6 maxBy | minBy

  • 根据某个字段 求最大值的那个对象
User orElseGet = userList.stream().collect(Collectors.maxBy(Comparator.comparing(user -> user.getId())))
                         .orElseGet(null);

User maxU = userList.stream().max(Comparator.comparing(user -> user.getId()))
                    .orElseGet(null);

1.7 summingLong

  • 求和Long: 对long型数据求和
Long aLong = userList.stream().collect(Collectors.summingLong(user -> user.getId()));
// 和下方产生的结果相同
Long collect3 = userList.stream().mapToLong(User::getId).sum();

1.8 summarizingLong

  • 总结Long:包含max min count avg
LongSummaryStatistics summaryStatistics = 
            userList.stream().collect(Collectors.summarizingLong(User::getId));
long count4 = summaryStatistics.getCount();
long max3 = summaryStatistics.getMax();
long min2 = summaryStatistics.getMin();
double average1 = summaryStatistics.getAverage();

2. 流操作

2.1 SummaryStatistics

  • 汇总统计
  // long类型 汇总统计 最大值 最小值等
        LongSummaryStatistics longSummaryStatistics =
                userList.stream().filter(i -> i.getAge() != null && i.getAge() > 20)
                        .sorted((o1, o2) -> -o1.getId().compareTo(o2.getId()))
                        .mapToLong(User::getId)
                        .summaryStatistics();
        long count3 = longSummaryStatistics.getCount();
        long max2 = longSummaryStatistics.getMax();
        long min1 = longSummaryStatistics.getMin();

2.2. reduce

// 使用 reduce 对 User对象结果集的年龄进行求和
Optional<Integer> reduce = userList.stream().map(User::getAge).reduce(Integer::sum);
Integer integer1 = reduce.orElse(0);

// 使用reduce对bigdecimal进行求和
List<BigDecimal> decimalList = new ArrayList<>();
        decimalList.add(new BigDecimal("0.020"));
        decimalList.add(new BigDecimal("0.0233"));
        decimalList.add(new BigDecimal("0.0244"));
        decimalList.add(new BigDecimal("0.02555"));
        Optional<BigDecimal> bigDecimal = decimalList.stream().reduce(BigDecimal::add);
        BigDecimal bigDecimal2 =
                decimalList.stream()
                        .reduce(BigDecimal::add)
                        .orElse(BigDecimal.ZERO)
                        .setScale(4, BigDecimal.ROUND_HALF_UP);

你可能感兴趣的:(JAVA后端面试,java,开发语言,spring)