FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java_upgrade/src/main/java/streams/FlatMapDemo.java at main · ps4/java_upgrade · GitHub
ps4
/
java_upgrade
Public
forked from
kousen/java_upgrade
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java_upgrade
/
src
/
main
/
java
/
streams
/
FlatMapDemo.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (41 loc) · 1.96 KB
Breadcrumbs
java_upgrade
/
src
/
main
/
java
/
streams
/
FlatMapDemo.java
Copy path
File metadata and controls
52 lines (41 loc) · 1.96 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
46
47
48
49
50
51
52
package
streams
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
import
java
.
util
.
stream
.
Stream
;
public
class
FlatMapDemo
{
public
static
void
main
(
String
[]
args
) {
Customer
sheridan
=
new
Customer
(
"Sheridan"
);
Customer
ivanova
=
new
Customer
(
"Ivanova"
);
Customer
garibaldi
=
new
Customer
(
"Garibaldi"
);
sheridan
.
addOrder
(
new
Order
(
1
))
.
addOrder
(
new
Order
(
2
))
.
addOrder
(
new
Order
(
3
));
ivanova
.
addOrder
(
new
Order
(
4
))
.
addOrder
(
new
Order
(
5
));
List
<
Customer
>
customers
=
Arrays
.
asList
(
sheridan
,
ivanova
,
garibaldi
);
// map for 1-1 customer to name --> Stream<String>
customers
.
stream
()
.
map
(
Customer
::
getName
)
// function<Customer,String>
.
forEach
(
System
.
out
::
println
);
// map 1-many customer to orders --> Stream<List<Order>>
customers
.
stream
()
.
map
(
Customer
::
getOrders
)
// function<Customer,List<Order>>
.
forEach
(
System
.
out
::
println
);
// map 1-many customer to orders.stream() --> Stream<Stream<Order>>
customers
.
stream
()
.
map
(
customer
->
customer
.
getOrders
().
stream
())
// function<Customer,Stream<Order>>
.
forEach
(
System
.
out
::
println
);
// stream() on an empty collection is already an empty Stream
customers
.
stream
()
.
flatMap
(
customer
->
customer
.
getOrders
().
stream
())
// function<Customer,Stream<Order>>
.
forEach
(
System
.
out
::
println
);
// flatMap 1-many customer to orders.stream() --> Stream<Order>
// Note: extra detail included just for illustration;
// stream() on an empty collection already returns an empty stream
customers
.
stream
()
.
flatMap
(
customer
->
customer
.
getOrders
().
size
() ==
0
?
Stream
.
empty
() :
customer
.
getOrders
().
stream
())
.
forEach
(
System
.
out
::
println
);
}
}
Back
|
FazBrowse Home
|
New Git URL