// 根据id去重
List<Person> unique = appleList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparingLong(Apple::getId))), ArrayList::new)
);

 

// 查找流中最大 最小值
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);

引自https://blog.csdn.net/lu930124/article/details/77595585

 

// 把对象本身当做value

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
}

相关文章:

  • 2021-09-24
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-12-04
  • 2021-07-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-01
  • 2021-06-29
  • 2022-12-23
  • 2021-12-23
  • 2021-09-12
相关资源
相似解决方案