Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FunctorExample {
- private static Optional<integer> readInt(final Scanner reader) {
- if (reader.hasNextInt()) {
- return Optional.of(reader.nextInt());
- } else {
- return Optional.empty();
- }
- }
- private static long compute(int i) {
- return i * i;
- }
- private static void display(final Optional<Long> result) {
- if (result.isPresent()) {
- System.out.println(result.get());
- } else {
- System.out.println("You don't give me an integer :-(");
- }
- }
- public static void main(String args[]) {
- System.out.println("Give me an integer");
- final Optional<Integer> optionalInt = readInt(new Scanner(System.in));
- Optional<Long> result = optionalInt.map(FunctorExample::compute);
- display(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement