FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java_upgrade/src/main/java/OptionalDemo.java at main · alcmontejo/java_upgrade · GitHub
alcmontejo
/
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
/
OptionalDemo.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
32 lines (25 loc) · 1.19 KB
Breadcrumbs
java_upgrade
/
src
/
main
/
java
/
OptionalDemo.java
Copy path
File metadata and controls
32 lines (25 loc) · 1.19 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
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
import
java
.
util
.
Optional
;
import
java
.
util
.
stream
.
Stream
;
public
class
OptionalDemo
{
public
static
void
main
(
String
[]
args
) {
Optional
<
Integer
>
first
=
Stream
.
of
(
3
,
1
,
4
,
1
,
5
,
9
)
.
filter
(
n
->
n
>
10
)
.
findFirst
();
System
.
out
.
println
(
first
);
// System.out.println(first.isPresent() ? (int) first.get() : 0);
int
defaultValue
=
0
;
System
.
out
.
println
(
first
.
orElse
(
defaultValue
));
System
.
out
.
println
(
first
.
orElseGet
(() ->
defaultValue
));
first
.
ifPresent
(
System
.
out
::
println
);
List
<
String
>
strings
=
Arrays
.
asList
(
"this"
,
"is"
,
"a"
,
"list"
,
"of"
,
"strings"
);
Optional
<
String
>
s
=
strings
.
stream
()
.
findFirst
();
System
.
out
.
println
(
s
.
orElse
(
"No string found; legal values are: "
+
strings
));
System
.
out
.
println
(
s
.
orElseGet
(() ->
"No string found; legal values are: "
+
strings
));
System
.
out
.
println
(
s
.
orElseThrow
(
IllegalArgumentException
::
new
));
// default constructor
System
.
out
.
println
(
s
.
orElseThrow
(() ->
new
IllegalArgumentException
(
"Not available"
)));
s
.
ifPresent
(
System
.
out
::
println
);
}
}
Back
|
FazBrowse Home
|
New Git URL