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
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
Map<String, User> stringUserMap = userList.stream().collect(Collectors.toMap(User::getName, user -> user));
1.5 averagingDouble
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 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
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
Optional<Integer> reduce = userList.stream().map(User::getAge).reduce(Integer::sum);
Integer integer1 = reduce.orElse(0);
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);