当前位置:网站首页>One bite of Stream(7)
One bite of Stream(7)
2022-07-20 19:44:00 【程序员·小李】
流的收集器
我们前面看到过这样的收集器:
List<Transaction> transactions = transactionStream.collect(Collectors.toList());
会把流中的数据依次填充到List中并返回。
1. 统计数量counting()
long howManyDishes = menu.stream().collect(Collectors.counting());
counting算收集器的一种,就是对流中元素的个数进行计数,也可以使用count简化
long howManyDishes = menu.stream().count();
2. 最大值、最小值maxBy() minBy()
Optional<Dish> mostCalorieDish = menu.stream()
.collect(Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)));
通过Comparator.comparingInt()可以生成一个比较器
Collectors.maxBy用于获取最大值
同理,最小值使用minBy()
3. 求和汇总summingInt()、summingLong()、summingDouble()
int total = menu.stream().collect(Collectors.summingInt(Dish::getCalories));
summingInt()用于求和,内部支持方法引用进行映射。
同样的,summingLong() summingDouble()也是类似的使用方法。
4. 求均值汇总averagingInt()、averagingLong()、averagingDouble()
double avg = menu.stream().collect(averagingInt(Dish::getCalories));
averagingInt对元素进行均值运算,接收方法引用进行映射
同样的,也支持averagingLong() averagingDouble()
5. 汇总summarizingInt()、summarizingLong()、summarizingDouble()
前面我们用到了counting()、maxBy()、minBy()、summingInt()、averagingInt(),现在使用summarizingInt()可以包含前面所有。
IntSummaryStatistics menuStatistics = menu.stream()
.collect(summarizingInt(Dish::getCalories));
IntSummaryStatistics就是统计数据的结果集:
IntSummaryStatistics{count=9, sum=4300, min=120, average=477.777778, max=800}
包含计数、求和、最小值、最大值、均值。
同样的,summarizingLong()、summarizingDouble()也可以使用,对应的结果集为LongSummaryStatistics、DoubleSummaryStatistics
6. 字符串拼接joining()
String shortMenu = menu.stream().collect(joining());
joining()默认调用toString()方法,使用StringBuilder进行拼接,也支持使用分隔符进行拼接。
String shortMenu = menu.stream().map(Dish::getName).collect(joining(", "));
边栏推荐
猜你喜欢
大健康产业商业供应链管理系统:采购管理规范化,提高企业采购效益
Study notes of pytorch deep learning practice: cyclic neural network (Advanced)
How to solve the problem that win11 excel file becomes a whiteboard icon?
2022年7月俄罗斯数据库排行榜:ClickHouse雄踞榜首,GigaBASE摘得榜眼
《PyTorch深度学习实践》学习笔记:循环神经网络(高级篇)
【刷题记录】16. 最接近的三数之和
《MySQL高级篇》四、索引的存储结构
单元测试,写起来到底有多痛?你会了吗
ECCV 2022 开源 | 给1万帧视频做目标分割
S2b2c mall system platform function module analysis, accelerate the implementation of digital transformation of pharmaceutical manufacturing enterprises
随机推荐
STL 笔记(十四):函数对象
The computer suddenly shows that there is only C disk, and other disks do not show ---- solution (very simple)
(22) blender source code analysis: mouse down message to window call process
Seven sorting knowledge points
truncate 与 delete 有何不同
在同花顺开户安全吗?佣金能给到多少?
MySQL Foundation
_分页查询
Commercial supply chain management system of big health industry: standardize procurement management and improve enterprise procurement efficiency
建立密切业务合作关系,供应商SRM系统助力企业做好新材料供应商品质管控
[3D target detection] pointpillars (II)
百度经典面试题——判定素数(如何优化?)
STL notes (IX): container BitSet
微软推出社交应用 Viva Engage,界面神似 Facebook
Can we have both good quality and low price?
STL 笔记(十三):关联性容器——映射
东方财富证券开户安全吗,佣金最低多少?
Simple student management system project: (add, delete, check, change, fuzzy check, paging check, upload, download, video import, current system time) -- "source code attached"
regular expression
正则表达式从入门到入坑