知识记录
使用Stream流获取唯一一个或首个的操作
1 | if (CollectionUtils.isNotEmpty(supplierList)) { |
分组
1 | Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId)); |
List转Map
1 | Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1)); |
total
1 | BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add); |
groupBy 实现的 list 转 map
1 | public static <T, K> Map<K, T> list2Map(super T, K> keyFunc) Collection<T> list, Function<? { |
去重
1 | List<Player> newList = playerList.stream().collect(Collectors .collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Player::getName))), ArrayList::new)); newList.forEach(System.out::println); |
创建空集合
1 | Collections.emptyList() |
获取集合最小值(为空添加默认值)
1 | evaluateBaseEntry.getValue().stream() |
list对象两个属性相乘在相加
1 | item.stream().map(p -> p.getQuantity().multiply(p.getUnitPrice())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO) |
Java Stream递归
1 | /** |
java stream 多属性过滤
1 | List<SupplierEvaluationYear> filter = supEvaluationYearList.stream() |