(1)Stream流:foreach遍历
list.foreach(s->{
}):
(2)Stream流:map取对象的有一列
List userNameList=userList.stream().map(User::getName).collect(Collectors.toList());
(3)Stream流:filter根据判断条件筛选出目标对象
List newUserList=userList.stream().filter(user->user.getName!=null).collect(Collectors.toList());
(4)Stream流:groupingBy
Map<String,List> map=userList.stream().collect(Collectors.groupingBy(User::getAge,Collectors.toList()));
(5)Stream流:求和int,long,double
double max=userList.stream().mapToDouble(User::getHeight).sum();
(6)Stream流:排序
升序:
List userList=userList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
降序:
List userList=userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
List userList=userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getCode)).collect(Collectors.toList());
工作中常用的Stream流
于 2023-04-19 17:32:14 首次发布