FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/streams/OptionalFlatMap.java at master · xinan-w/OnJava8-Examples · GitHub
xinan-w
/
OnJava8-Examples
Public
forked from
BruceEckel/OnJava8-Examples
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
OnJava8-Examples
/
streams
/
OptionalFlatMap.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (68 loc) · 1.57 KB
Breadcrumbs
OnJava8-Examples
/
streams
/
OptionalFlatMap.java
Copy path
File metadata and controls
73 lines (68 loc) · 1.57 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// streams/OptionalFlatMap.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import
java
.
util
.*;
import
java
.
util
.
stream
.*;
import
java
.
util
.
function
.*;
class
OptionalFlatMap
{
static
String
[]
elements
= {
"12"
,
""
,
"23"
,
"45"
};
static
Stream
<
String
>
testStream
() {
return
Arrays
.
stream
(
elements
);
}
static
void
test
(
String
descr
,
Function
<
String
,
Optional
<
String
>>
func
) {
System
.
out
.
println
(
" ---( "
+
descr
+
" )---"
);
for
(
int
i
=
0
;
i
<=
elements
.
length
;
i
++) {
System
.
out
.
println
(
testStream
()
.
skip
(
i
)
.
findFirst
()
.
flatMap
(
func
));
}
}
public
static
void
main
(
String
[]
args
) {
test
(
"Add brackets"
,
s
->
Optional
.
of
(
"["
+
s
+
"]"
));
test
(
"Increment"
,
s
-> {
try
{
return
Optional
.
of
(
Integer
.
parseInt
(
s
) +
1
+
""
);
}
catch
(
NumberFormatException
e
) {
return
Optional
.
of
(
s
);
}
});
test
(
"Replace"
,
s
->
Optional
.
of
(
s
.
replace
(
"2"
,
"9"
)));
test
(
"Take last digit"
,
s
->
Optional
.
of
(
s
.
length
() >
0
?
s
.
charAt
(
s
.
length
() -
1
) +
""
:
s
));
}
}
/* Output:
---( Add brackets )---
Optional[[12]]
Optional[[]]
Optional[[23]]
Optional[[45]]
Optional.empty
---( Increment )---
Optional[13]
Optional[]
Optional[24]
Optional[46]
Optional.empty
---( Replace )---
Optional[19]
Optional[]
Optional[93]
Optional[45]
Optional.empty
---( Take last digit )---
Optional[2]
Optional[]
Optional[3]
Optional[5]
Optional.empty
*/
Back
|
FazBrowse Home
|
New Git URL