[Java] Check the difference between orElse and orElseGet with IntStream

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

About the name orElse ...

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

[Java] Check the difference between orElse and orElseGet with IntStream
[Java] Difference between Intstream range and rangeClosed
[Java] Understand the difference between List and Set
[Java] Difference between == and equals
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
[Java] Difference between Hashmap and HashTable
Difference between Java and JavaScript (how to find the average)
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
[Java] Difference between Closeable and AutoCloseable
The difference between programming with Ruby classes and programming without it
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Regarding the difference between Java array and ArrayList, I compared and corresponded methods with similar functions.
[Java] What is the difference between form, entity and dto? [Bean]
Difference between final and Immutable in Java
About the difference between irb and pry
Difference between int and Integer in Java
Understand the difference between int and Integer and BigInteger in java and float and double
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
Difference between next () and nextLine () in Java Scanner
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Summarize the differences between C # and Java writing
[Java] Difference between "final variable" and "immutable object"
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
What is the difference between System Spec and Feature Spec?
About the difference between classes and instances in Ruby
[Java] Difference between static final and final in member variables
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
What is the difference between skip and pending? [RSpec]
Prepare the environment for java11 and javaFx with Ubuntu 18.4
[Rails] I investigated the difference between redirect_to and render.
[Swift] UITextField taught me the difference between nil and ""
[Java beginner] Difference between length and length () ~ I don't know ~
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
[Java] The difference between the Stream.of () and Arrays.stream () methods that you do not know unexpectedly
Difference between product and variant
Find the address class and address type from the IP address with Java
Difference between redirect_to and render
[Rails] What is the difference between bundle install and bundle update?
Difference between puts and print
Difference between redirect_to and render
Command to check the number and status of Java threads
Difference between CUI and GUI
About the difference between "(double quotation)" and "single quotation" in Ruby
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between element 0, null and empty string (check in list)
[Ruby] About the difference between 2 dots and 3 dots of range object.
Difference between class and instance
What is the difference between an action and an instance method?
Difference between ArrayList and LinkedList
Difference between render and redirect_to