我在 intellij 中有这段代码:
return collection.stream().anyMatch(annotation ->
method.isAnnotationPresent(annotation));
编译器告诉我“method.isAnnotationPresent(annotation)”可以用方法引用替换,我不知道该怎么做,因为它有一个参数。
有人知道怎么做吗?
最佳答案
您可以替换您的代码以使用方法引用(查看 here ),如下所示:
return collection.stream().anyMatch(method::isAnnotationPresent);
基本上,您正在为 Lambda 表达式(anyMatch
方法接受 Predicate ) 并且流中的值将自 Action 为参数传递给 anyMatch
方法。
关于java - 可以用java中使用反射的方法引用替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43312574/