Java8 method reference used as Function object to combine functions

前端 未结 7 1618
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 15:02

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         


        
7条回答
  •  情书的邮戳
    2020-12-14 15:30

    You can use a cast

    Stream.of("ciao", "hola", "hello")
            .map(((Function) String::length)
                    .andThen(n -> n * 2))
            .forEach(System.out::println);
    

    prints

    8
    8
    10
    

提交回复
热议问题