Java8 关于stream.foreach()和stream.peek()的区别解析
该思考来源于日常工作中,特记此心得。
思考:如何快速将list中的每个item内部属性值改变并进行其他流体操作呢?
下面做个测试:如何先在list中统一改变某属性的值,然后再根据某个属性取出该属性值最小的对象
1:随便新建一个测试bean:

1 package com.dev.model;
2
3 import javax.persistence.*;
4
5 public class Aopi {
6 /**
7 * id
8 */
9 @Id
10 @GeneratedValue(strategy = GenerationType.IDENTITY)
11 private Integer id;
12
13 /**
14 * 姓名
15 */
16 private String name;
17
18 /**
19 * 年龄
20 */
21 private Integer age;
22
23 /**
24 * 获取id
25 *
26 * @return id - id
27 */
28 public Integer getId() {
29 return id;
30 }
31
32 /**
33 * 设置id
34 *
35 * @param id id
36 */
37 public void setId(Integer id) {
38 this.id = id;
39 }
40
41 /**
42 * 获取姓名
43 *
44 * @return name - 姓名
45 */
46 public String getName() {
47 return name;
48 }
49
50 /**
51 * 设置姓名
52 *
53 * @param name 姓名
54 */
55 public void setName(String name) {
56 this.name = name;
57 }
58
59 /**
60 * 获取年龄
61 *
62 * @return age - 年龄
63 */
64 public Integer getAge() {
65 return age;
66 }
67
68 /**
69 * 设置年龄
70 *
71 * @param age 年龄
72 */
73 public void setAge(Integer age) {
74 this.age = age;
75 }
76
77 public Aopi(String name, Integer age) {
78 this.name = name;
79 this.age = age;
80 }
81
82 public Aopi() {
83 }
84
85 @Override
86 public String toString() {
87 return "Aopi{" +
88 "id=" + id +
89 ", name='" + name + '\'' +
90 ", age=" + age +
91 '}';
92 }
93 }
2:新建一个单元测试:
@Test
public void test01() {
List<Aopi> aopiList = Lists.newArrayList(); Aopi aopi = new Aopi("1", 1);
Aopi aop2 = new Aopi("2", 2);
Aopi aop3 = new Aopi("3", 3);
Aopi aop4 = new Aopi("4", 4); aopiList.addAll(Arrays.asList(aopi, aop2, aop3, aop4)); //第一种方式
aopiList.forEach(item -> item.setName(item.getName() + "_test"));
System.out.println(
aopiList.stream().min((o1, o2) -> {
if (Objects.equals(o1.getAge(), o2.getAge()))
return 0;
return o1.getAge() > o2.getAge() ? 1 : -1;
}).get().toString()
); System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"); //第二种方式
System.out.println(
aopiList.stream().peek(item -> item.setName(item.getName() + "_test")).min((o1, o2) -> {
if (Objects.equals(o1.getAge(), o2.getAge()))
return 0;
return o1.getAge() > o2.getAge() ? 1 : -1;
}).get().toString()
); }
notice1:测试第一种方式注释掉第二种,反之亦如此
notice2:list.stream().foreach -> list.foreach()
3:看测试结果:
第一种测试结果:

第二种测试结果:

结论:
(1):使用stream.foreach也可以更改list中的每个item的内部属性值等等,但是要进行“二次流处理”,才能得到list中最小的item(根据age筛选)
(2):stream.peek比stream.foreach()可以跟直接拿到最小的item(根据age筛选)
原因:
(1):stream.foreach的操作是void的,除了更改属性值还可以进行其他操作等。因此要做“二次流处理”。
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
(1):stream.peek的操作是返回一个新的stream的,且设计的初衷是用来debug调试的,因此使用steam.peek()必须对流进行一次处理再产生一个新的stream。
/**
* Returns a stream consisting of the elements of this stream, additionally
* performing the provided action on each element as elements are consumed
* from the resulting stream.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* <p>For parallel stream pipelines, the action may be called at
* whatever time and in whatever thread the element is made available by the
* upstream operation. If the action modifies shared state,
* it is responsible for providing the required synchronization.
*
* @apiNote This method exists mainly to support debugging, where you want
* to see the elements as they flow past a certain point in a pipeline:
* <pre>{@code
* Stream.of("one", "two", "three", "four")
* .filter(e -> e.length() > 3)
* .peek(e -> System.out.println("Filtered value: " + e))
* .map(String::toUpperCase)
* .peek(e -> System.out.println("Mapped value: " + e))
* .collect(Collectors.toList());
* }</pre>
*
* @param action a <a href="package-summary.html#NonInterference">
* non-interfering</a> action to perform on the elements as
* they are consumed from the stream
* @return the new stream
*/
Stream<T> peek(Consumer<? super T> action);
Java8 关于stream.foreach()和stream.peek()的区别解析的更多相关文章
- [三]java8 函数式编程Stream 概念深入理解 Stream 运行原理 Stream设计思路
Stream的概念定义 官方文档是永远的圣经~ 表格内容来自https://docs.oracle.com/javase/8/docs/api/ Package java.util.s ...
- java8 for ,forEach ,lambda forEach , strean forEach , parller stream forEach, Iterator性能对比
java8 for ,forEach ,Iterator,lambda forEach ,lambda strean forEach , lambda parller stream forEach性 ...
- Java 8 Stream Api 中的 peek 操作
1. 前言 我在Java8 Stream API 详细使用指南[1] 中讲述了 [Java 8 Stream API]( "Java 8 Stream API") 中 map 操作 ...
- 十分钟学会Java8的lambda表达式和Stream API
01:前言一直在用JDK8 ,却从未用过Stream,为了对数组或集合进行一些排序.过滤或数据处理,只会写for循环或者foreach,这就是我曾经的一个写照. 刚开始写写是打基础,但写的多了,各种乏 ...
- Java8新特性之三:Stream API
Java8的两个重大改变,一个是Lambda表达式,另一个就是本节要讲的Stream API表达式.Stream 是Java8中处理集合的关键抽象概念,它可以对集合进行非常复杂的查找.过滤.筛选等操作 ...
- java8中Lambda表达式和Stream API
一.Lambda表达式 1.语法格式 Lambda是匿名函数,可以传递代码.使用“->”操作符,改操作符将lambda分成两部分: 左侧:指定了 Lambda 表达式需要的所有参数 右侧:指定了 ...
- 十分钟学会Java8:lambda表达式和Stream API
Java8 的新特性:Lambda表达式.强大的 Stream API.全新时间日期 API.ConcurrentHashMap.MetaSpace.总得来说,Java8 的新特性使 Java 的运行 ...
- Java8中的 lambda 和Stream API
前言 由于项目中用到了比较多有关于 Java8 中新的东西,一开始自己只是会写,但是写起来不太顺,然后就在网上找到了一个很好的关于Java8新特性的视频,所以就进行了学习了一下,以下是自己对 la ...
- Java8的lambda表达式和Stream API
一直在用JDK8 ,却从未用过Stream,为了对数组或集合进行一些排序.过滤或数据处理,只会写for循环或者foreach,这就是我曾经的一个写照. 刚开始写写是打基础,但写的多了,各种乏味,非过来 ...
随机推荐
- Eclipse无法查看Servlet源代码的解决方案
在Apache官方网站中选择你对应的tomacat版本下载对应的Tomcat的源码 下载Source Code Distributions下的zip 将下载的zip文件复制到lib文件夹下 在提示页面 ...
- B - B Saruman's Army(贪心)
在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...
- AcWing 250 磁力快(分块)
题目传送门 在一片广袤无垠的原野上,散落着N块磁石. 每个磁石的性质可以用一个五元组(x,y,m,p,r)描述,其中x,y表示其坐标,m是磁石的质量,p是磁力,r是吸引半径. 若磁石A与磁石B的距离不 ...
- 【bzoj 1190】梦幻岛宝珠(DP)
这题是在01背包问题的基础上,扩充了重量,需要用时间换空间. 思路: 1.仔细看题,注意到重量wi为a*2^b(a<=10,b<=30),很容易想到要按 b 分开做背包的DP.接下来的重点 ...
- SpringBoot引入openfeign 报错:spring-cloud-starter-openfeign:unknown
现象: 1.maven报错:Cannot resolve org.springframework.cloud:spring-cloud-starter-openfeign:unknown 解决: 在h ...
- leetcode28 strstr kmp bm sunday
字符串匹配有KMP,BM,SUNDAY算法. 可见(https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa- ...
- 统计学三大相关性系数:pearson,spearman,kendall
目录 person correlation coefficient(皮尔森相关性系数-r) spearman correlation coefficient(斯皮尔曼相关性系数-p) kendall ...
- matplotlib 图标显示中文
matplotlib 显示中文 Method_1: # 添上: plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcPara ...
- JVM系列之一 JVM的基础概念与内存区域
前言 作为一名 Java 语言的使用者,学习 JVM 有助于解决程序运行过程中出现的问题.写出性能更高的代码. 可以说:学好 JVM 是成为中高级 Java 工程师的必经之路. 有感于从未整理归纳 J ...
- 图解 git stash
图解 git stash # 暂存本地 变化 $ git stash # 复原 $ git stash pop The "git stash" command can help y ...