本文介绍: Collectors类实现了很多归约操作,例如,toList、toMap、toSet、counting、summingInt、averagingInt、maxBy、minBy、joining、groupingBy等等。map方法用于映射每个元素到对应的结果,有返回值,返回的是一个新流,可以对这个流进一步操作。sorted方法用于对流进行排序,例如,sorted()用于升序,sorted(Comparator.reverseOrder())用于降序。skip则是返回除前n个元素的元素。min用于获取最小值。

(1)Stream

Stream(流)是一个来自数据源的元素队列并支持聚合操作。
forEach方法用来迭代流中的每个数据,没有返回值。map方法用于映射每个元素到对应的结果,有返回值,返回的是一个新流,可以对这个流进一步操作。
filter方法用于通过设置的条件过滤出元素。limit返回前n个元素。skip则是返回除前n个元素的元素。distinct方法用于去重。
sorted方法用于对流进行排序,例如,sorted()用于升序,sorted(Comparator.reverseOrder())用于降序。
max用于获取最大值。min用于获取最小值。count用于计算元素数量。sum用于求和。
anyMatch满足一个条件则返回true。allMatch满足所有条件则返回true。noneMatch不满足所有条件则返回true。findFirst返回第一个元素。findAny返回任意一个元素。
collect,收集流。Collectors类实现了很多归约操作,例如,toList、toMap、toSet、counting、summingInt、averagingInt、maxBy、minBy、joining、groupingBy等等。
reduce,聚合,将流中全部的数据聚合成一个值。

(2)测试

User tom = new User(1, "tom", 2, new Date());
User jerry = new User(3, "jerry", 1, new Date());
User diana = new User(2, "diana", 3, new Date());
List<User> userList = Arrays.asList(tom, jerry, diana);
log.info("list:{}", userList);
userList.stream().forEach(u -> {
	if (u.getAge() > 1) {
		log.info("{}", u);
	}
});
List<Integer> list = userList.stream().map(u -> u.getAge() + 1).limit(10).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
log.info("list:{}", list);
Map<Integer, Object> map = userList.stream().collect(Collectors.toMap(User::getId, User::getName));
log.info("map:{}", map);
boolean flag = userList.stream().anyMatch(u -> u.getAge() > 5);
log.info("flag:{}", flag);
User any = userList.stream().findAny().get();
log.info("any:{}", any);
User max = userList.stream().max((u1, u2) -> u1.getAge() - u2.getAge()).get();
log.info("max:{}", max);
Integer sum = userList.stream().map(u -> u.getAge()).reduce(0, (a1, a2) -> {
	return a1 + a2;
}).intValue();
log.info("sum:{}", sum);
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - list:[User(id=1, name=tom, age=2, birth=Fri Jan 26 14:45:26 CST 2024), User(id=3, name=jerry, age=1, birth=Fri Jan 26 14:45:26 CST 2024), User(id=2, name=diana, age=3, birth=Fri Jan 26 14:45:26 CST 2024)] 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - User(id=1, name=tom, age=2, birth=Fri Jan 26 14:45:26 CST 2024) 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - User(id=2, name=diana, age=3, birth=Fri Jan 26 14:45:26 CST 2024) 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - list:[4, 3, 2] 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - map:{1=tom, 2=diana, 3=jerry} 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - flag:false 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - any:User(id=1, name=tom, age=2, birth=Fri Jan 26 14:45:26 CST 2024) 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - max:User(id=2, name=diana, age=3, birth=Fri Jan 26 14:45:26 CST 2024) 
2024-01-26 14:45:26 [http-nio-8080-exec-4] INFO  cn.hwd.controller.TestController - sum:6 

原文地址:https://blog.csdn.net/vinegar93/article/details/135866182

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_63241.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注