各种Stream流操作

  • 过滤soList中Object的Name字段为空的情况
List<Object> soList = Lists.newArrayList();List<Object> list = soList.stream().filter(item -> item.getName() != null).collect(Collectors.toList());

  • 取soList列表根据对象某个字段 去重
List<Object> soList = Lists.newArrayList()//distinct() 去重 List<Integer> maxDueDayList2 = soList.stream().map(Object::getMaxDueDay).distinct().collect(Collectors.toList());

  • 计算一个List对象中某个字段总和
int total = list.stream().mapToInt(User::getAge).sum();//上下等同int ageSum = userList.stream().collect(Collectors.summingInt(User::getAge));
  • 计算一个List对象中某个字段的和、最大值、最小值、平均值、总个数
double doublesum = listUsers.stream().mapToDouble(Users::getAge).sum();//和
int intmax = listUsers.stream().mapToInt(Users::getAge).max().getAsInt();//最大
int intmin = listUsers.stream().mapToInt(Users::getAge).min().getAsInt();//最小
double avg = listUsers.stream().mapToDouble(Users::getAge).average().getAsDouble();//平均
posted @ 2022-03-15 10:30  倦了_诗书  阅读(51)  评论(0)    收藏  举报