FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-base/java-8future/src/test/java/ConsumerTest.java at master · singgel/java-base · GitHub
singgel
/
java-base
Public
Notifications
You must be signed in to change notification settings
Fork
28
Star
39
Code
Issues
0
Pull requests
6
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-base
/
java-8future
/
src
/
test
/
java
/
ConsumerTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
45 lines (39 loc) · 1.39 KB
Breadcrumbs
java-base
/
java-8future
/
src
/
test
/
java
/
ConsumerTest.java
Copy path
File metadata and controls
45 lines (39 loc) · 1.39 KB
Raw
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import
org
.
junit
.
Test
;
import
java
.
util
.
function
.
Consumer
;
import
java
.
util
.
stream
.
Stream
;
/**
* @author heks
* @description: TODO
* @date 2020/11/10
*/
public
class
ConsumerTest
{
/**
* consumer接口测试
*/
@
Test
public
void
test_Consumer
() {
//① 使用consumer接口实现方法
Consumer
<
String
>
consumer
=
new
Consumer
<
String
>() {
@
Override
public
void
accept
(
String
s
) {
System
.
out
.
println
(
s
);
}
};
Stream
<
String
>
stream
=
Stream
.
of
(
"aaa"
,
"bbb"
,
"ddd"
,
"ccc"
,
"fff"
);
stream
.
forEach
(
consumer
);
System
.
out
.
println
(
"********************"
);
//② 使用lambda表达式,forEach方法需要的就是一个Consumer接口
stream
=
Stream
.
of
(
"aaa"
,
"bbb"
,
"ddd"
,
"ccc"
,
"fff"
);
Consumer
<
String
>
consumer1
= (
s
) ->
System
.
out
.
println
(
s
);
//lambda表达式返回的就是一个Consumer接口
stream
.
forEach
(
consumer1
);
//更直接的方式
//stream.forEach((s) -> System.out.println(s));
System
.
out
.
println
(
"********************"
);
//③ 使用方法引用,方法引用也是一个consumer
stream
=
Stream
.
of
(
"aaa"
,
"bbb"
,
"ddd"
,
"ccc"
,
"fff"
);
Consumer
consumer2
=
System
.
out
::
println
;
stream
.
forEach
(
consumer
);
//更直接的方式
//stream.forEach(System.out::println);
}
}
Back
|
FazBrowse Home
|
New Git URL