fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println(combine(Optional.empty(), Optional.empty()));
  13. System.out.println(combine(Optional.of(1), Optional.empty()));
  14. System.out.println(combine(Optional.empty(), Optional.of(2)));
  15. System.out.println(combine(Optional.of(1), Optional.of(2)));
  16. }
  17.  
  18. private static Optional<Integer> combine(Optional<Integer> a, Optional<Integer> b) {
  19. return a.map(x -> b.map(y -> x + y).orElse(x)).or(() -> b);
  20. }
  21. }
Success #stdin #stdout 0.1s 48332KB
stdin
Standard input is empty
stdout
Optional.empty
Optional[1]
Optional[2]
Optional[3]