I recently had an interesting conversation with my colleagues about the usage of Optional's in Java. It all revolves around this article.
So, I quickly sketched the following code to highlight the idea:
import java.util.Optional;
public class OptionalEvaluationTest {
public static void main(String[] args) {
test1(); // non lazy execution of orElse().
test2();
test3();
test4();
}
private static void test1() {
System.out.println("Test 1");
Optional<Integer> oi = Optional.ofNullable(3);
oi.map(OptionalEvaluationTest::intToString)
.orElse(defaultString());
System.out.println("----");
}
private static void test2() {
System.out.println("Test 2");
Optional<Integer> oi = Optional.ofNullable(null);
oi.map(OptionalEvaluationTest::intToString)
.orElse(defaultString());
System.out.println("----");
}
private static void test3() {
System.out.println("Test 3");
Optional<Integer> oi = Optional.ofNullable(3);
oi.map(OptionalEvaluationTest::intToString)
.orElseGet(OptionalEvaluationTest::defaultString);
System.out.println("----");
}
private static void test4() {
System.out.println("Test 4");
Optional<Integer> oi = Optional.ofNullable(null);
oi.map(OptionalEvaluationTest::intToString)
.orElseGet(OptionalEvaluationTest::defaultString);
System.out.println("----");
}
private static String intToString(int i) {
System.out.println("converter called!");
return Integer.toString(i);
}
private static String defaultString() {
System.out.println("default called!");
return "default";
}
}
which yields the following result:
Test 1
converter called!
default called!
----
Test 2
default called!
----
Test 3
converter called!
----
Test 4
default called!
----
What we see is that .orElse() is executed always, aka is not lazy executed. This may become a performance issue if used unwisely. Tend to prefer .orElseGet() instead.
No comments:
Post a Comment