Is there a way in Java8 to use a method reference as a Function
object to use its methods, something like:
Stream.of(\"ciao\", \"hola\", \"hello
We can use reduce
function to combine multiple functions into one.
public static void main(String[] args) {
List<Function<String, String>> normalizers = Arrays.asList(
str -> {
System.out.println(str);
return str;
},
String::toLowerCase,
str -> {
System.out.println(str);
return str;
},
String::trim,
str -> {
System.out.println(str);
return str;
});
String input = " Hello World ";
normalizers.stream()
.reduce(Function.identity(), (a, b) -> a.andThen(b))
.apply(input);
}
Output:
Hello World
hello world
hello world