Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
27 lines (20 loc) · 795 Bytes

File metadata and controls

27 lines (20 loc) · 795 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.hks.lambda;
import org.junit.Test;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PeekTest {
/**
* forEach 是 terminal 操作,因此它执行后,Stream 的元素就被“消费”掉了,你无法对一个 Stream 进行两次 terminal 运算。
*
* 相反,具有相似功能的 intermediate 操作 peek 可以达到上述目的。
*/
@Test
public void foreachPeekTest(){
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());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.