简体   繁体   English

方法引用vs lambda表达式

[英]method reference vs lambda expression

I want to replace lambda expression by method reference in the below example : 我想通过以下示例中的方法引用替换lambda表达式:

 public class Example {

        public static void main(String[] args) {
            List<String> words = Arrays.asList("toto.", "titi.", "other");
         //lambda expression in the filter (predicate)
            words.stream().filter(s -> s.endsWith(".")).forEach(System.out::println);
        }
   }

I want to write a something like this : 我想写一下这样的东西:

words.stream().filter(s::endsWith(".")).forEach(System.out::println);

is it possible to transform any lambda expression to method reference. 是否可以将任何lambda表达式转换为方法引用。

There is no way “to transform any lambda expression to method reference”, but you can implement a factory for a particular target type, if this serves recurring needs: 没有办法“将任何lambda表达式转换为方法引用”,但您可以为特定目标类型实现工厂,如果这样可以满足重复需求:

public static <A,B> Predicate<A> bind2nd(BiPredicate<A,B> p, B b) {
    return a -> p.test(a, b);
}

with this, you can write 有了这个,你可以写

words.stream().filter(bind2nd(String::endsWith, ".")).forEach(System.out::println);

but actually, there's no advantage. 但实际上,没有优势。 Technically, a lambda expression does exactly what you want, there's the minimum necessary argument transformation code, expressed as the lambda expression's body, compiled into a synthetic method and a method reference to that synthetic code. 从技术上讲,lambda表达式完全符合您的要求,它是最小的必要参数转换代码,表示为lambda表达式的主体,编译为合成方法和对该合成代码的方法引用。 The syntax 语法
s -> s.endsWith(".") also is already the smallest syntax possible to express that intent. s -> s.endsWith(".")也是表达该意图的最小语法。 I doubt that you can find a smaller construct that would still be compatible with the rest of the Java programming language. 我怀疑你能找到一个仍然与Java编程语言的其余部分兼容的小构造。

You can use selectWith() from Eclipse Collections . 您可以使用Eclipse Collections中的 selectWith() selectWith() takes a Predicate2 which takes 2 parameters instead of a Predicate . selectWith()接受一个Predicate2 ,它接受2个参数而不是Predicate The second parameter to selectWith() gets passed as the second parameter to the Predicate2 every time it's called, once per item in the iterable. 每次调用时, selectWith()的第二个参数作为第二个参数传递给Predicate2 ,每个项目在iterable中传递一次。

MutableList<String> words = Lists.mutable.with("toto.", "titi.", "other");
words.selectWith(String::endsWith, ".").each(System.out::println);

By default Eclipse Collections is eager, if you want to iterate lazily then you can use asLazy() 默认情况下,Eclipse Collections是急切的,如果你想懒惰地迭代,那么你可以使用asLazy()

words.asLazy().selectWith(String::endsWith, ".").each(System.out::println);

If you can't change from List : 如果您无法从List更改:

List<String> words = Arrays.asList("toto.", "titi.", "other");
ListAdapter.adapt(words).selectWith(String::endsWith, ".").each(System.out::println);

Eclipse Collections' RichIterable has several other *With methods which work well with method references, including rejectWith() , partitionWith() , detechWith() , anySatisfyWith() , allSatisfyWith() , noneSatisfyWith() , collectWith() Eclipse Collections的RichIterable还有其他几个* With方法可以很好地处理方法引用,包括rejectWith()partitionWith()detechWith()anySatisfyWith()allSatisfyWith()noneSatisfyWith()collectWith()

Note: I am a contributor to Eclipse Collections. 注意:我是Eclipse Collections的贡献者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM