File tree Expand file tree Collapse file tree 1 file changed +4
-9
lines changed
src/main/java/com/winterbe/java8/samples/stream Expand file tree Collapse file tree 1 file changed +4
-9
lines changed Original file line number Diff line number Diff line change 55
66/**
77 * Examples how to avoid null checks with Optional:
8- *
98 * http://winterbe.com/posts/2015/03/15/avoid-null-checks-in-java/
109 *
1110 * @author Benjamin Winterberg
@@ -46,24 +45,20 @@ public static <T> Optional<T> resolve(Supplier<T> resolver) {
4645 try {
4746 T result = resolver .get ();
4847 return Optional .ofNullable (result );
49- }
50- catch (NullPointerException e ) {
48+ } catch (NullPointerException e ) {
5149 return Optional .empty ();
5250 }
5351 }
5452
5553 private static void test3 () {
5654 Outer outer = new Outer ();
57- resolve (() -> outer .getNested ().getInner ().getFoo ())
58- .ifPresent (System .out ::println );
55+ resolve (() -> outer .getNested ().getInner ().getFoo ()).ifPresent (System .out ::println );
5956 }
6057
6158 private static void test2 () {
6259 Optional .of (new Outer ())
63- .map (Outer ::getNested )
64- .map (Nested ::getInner )
65- .map (Inner ::getFoo )
66- .ifPresent (System .out ::println );
60+ .map (Outer ::getNested ).map (Nested ::getInner )
61+ .map (Inner ::getFoo ).ifPresent (System .out ::println );
6762 }
6863
6964 private static void test1 () {
You can’t perform that action at this time.
0 commit comments