Advertisement
Guest User

Untitled

a guest
Oct 25th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. public class FunctorExample {
  2. private static Optional<integer> readInt(final Scanner reader) {
  3. if (reader.hasNextInt()) {
  4. return Optional.of(reader.nextInt());
  5. } else {
  6. return Optional.empty();
  7. }
  8. }
  9.  
  10. private static long compute(int i) {
  11. return i * i;
  12. }
  13.  
  14. private static void display(final Optional<Long> result) {
  15. if (result.isPresent()) {
  16. System.out.println(result.get());
  17. } else {
  18. System.out.println("You don't give me an integer :-(");
  19. }
  20. }
  21.  
  22. public static void main(String args[]) {
  23. System.out.println("Give me an integer");
  24. final Optional<Integer> optionalInt = readInt(new Scanner(System.in));
  25. Optional<Long> result = optionalInt.map(FunctorExample::compute);
  26. display(result);
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement