osElse Returns the int value of the argument when OptionalInt is null
System.out.println(IntStream.of(1, 2, 3).findFirst().orElse(100)); //=>Returns the 1 earned by findFirst
System.out.println(IntStream.of().findFirst().orElse(100)); //=>Returns 100 argument because it cannot be acquired by findFirst
orElseGet Execute the argument Supplier when OptionalInt is null
System.out.println(IntStream.of(1, 2, 3).findFirst().orElseGet(() -> 100)); //=>Returns the 1 earned by findFirst
System.out.println(IntStream.of().findFirst().orElseGet(() -> 100)); //=>Because you can't get it with findFirst()->100 is executed to get the return value of 100
Probably named after a method called ifPresent Since ifPresent is a void method, it cannot coexist with orElse in the method chain.
//The following is a compilation error
ifPresent(System.out::println).orElse(0);
Recommended Posts