概述
写Java接口的朋友都知道,Java 8的更新,经常会用到过滤 list 里的数据,本文就对List使用Stream流进行集合Collection的各种运算做一个汇总!
优势:
Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作,或者大批量数据操作。
通常我们需要多行代码才能完成的操作,借助于Stream流式处理可以很简单的实现。
各种Stream流操作:
过滤soList中Object的Name字段为空的情况
List soList = Lists.newArrayList();List list = soList.stream().filter(item -> item.getName() != null).collect(Collectors.toList());
取soList列表根据对象某个字段 去重
List soList = Lists.newArrayList()//distinct() 去重 List 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();//平均
//计算一个number类型的List对象Integer[] integerArray = {1, 3, 5, 10, 18};List list = ne.........
最后
以上就是坚定水池为你收集整理的bigdecimal 平均数_List使用Stream流进行集合Collection的各种运算汇总:对BigDecimal求和,某个字段的和、最大值、最小值、平均值,字段去重,过滤等...的全部内容,希望文章能够帮你解决bigdecimal 平均数_List使用Stream流进行集合Collection的各种运算汇总:对BigDecimal求和,某个字段的和、最大值、最小值、平均值,字段去重,过滤等...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复