Advertisement
DulcetAirman

Function.andThen

May 5th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. package ch.claude_martin;
  2.  
  3. import java.util.Scanner;
  4. import java.util.function.Function;
  5.  
  6. public class SomeClass {
  7.     public static void main(String[] args) {
  8.         Function<String, String> trim = String::trim;
  9.         Function<CharSequence, Long> length = s -> s.codePoints().count();
  10.  
  11.         Function<String, Number> trimmedLength = trim.andThen(length);
  12.         System.out.println("Type something and hit <enter>.");
  13.         try (Scanner scanner = new Scanner(System.in)) {
  14.             while (true) {
  15.                 String line = scanner.nextLine();
  16.                 Number len = trimmedLength.apply(line);
  17.                 if (0 == len.longValue())
  18.                     break;
  19.                 System.out.format("The trimmed input was %d code points long.%n", len);
  20.             }
  21.         }
  22.         System.out.println("Good Bye");
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement