分组
Map<Integer, List<Apple>> groupBy = appleList.stream()
.collect(Collectors.groupingBy(Apple::getId));
Map<Boolean, List<Person>> part = personList.stream()
.collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
List 转 Map
Map<Integer, Apple> appleMap = appleList.stream()
.collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
过滤
List<Apple> filterList = appleList.stream()
.filter(a -> a.getName().equals("香蕉")).collect(Collectors.toList());
求和
BigDecimal totalMoney = appleList.stream()
.map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
最大值
Optional<Dish> maxDish = Dish.menu.stream().
collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories)));
maxDish.ifPresent(System.out::println);
最小值
Optional<Dish> minDish = Dish.menu.stream().
collect(Collectors.minBy(Comparator.comparing(Dish::getCalories)));
minDish.ifPresent(System.out::println);
去重
List<Person> unique = appleList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparingLong(Apple::getId))), ArrayList::new)
);
原文
java8 快速实现List转map 、分组、过滤等操作