java - 可以用java中使用反射的方法引用替换

标签 java intellij-idea reflection java-8 method-reference

我在 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/

相关文章:

java - 使用反射实例化实现特定接口(interface)的 Java 类

Java:制作窗口点击(包括文本/图像)

java - Riak中按属性查询

Java TargetDataLine 没有接收到任何音频?

java - 如何禁用 intellij 自动更改我的代码格式?

objective-c - 有没有办法将整个参数列表传递给 Objective C 中的另一个方法?

java - 当我调用 BufferedReader 时,我的程序卡住了

Java Swing : alignment of right element in JSplitPane

intellij-idea - IntelliJ 方法列表

html - css 反射问题 - 反射图像的错误部分