java 可以使用java中的反射替换为方法引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43312574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can be replaced with method reference using reflection in java
提问by Motomine
I have this code in intellij:
我在intellij中有这个代码:
return collection.stream().anyMatch(annotation ->
method.isAnnotationPresent(annotation));
And the compiler tells me that "method.isAnnotationPresent(annotation)" can be replaced with method reference and I can't figure out how to do it because it has an argument.
编译器告诉我“method.isAnnotationPresent(annotation)”可以用方法引用替换,我不知道怎么做,因为它有一个参数。
Does anyone know how to make that?
有谁知道怎么做?
回答by developer
You can replace your code to use the method reference (look here) as shown below:
您可以替换您的代码以使用方法参考(查看此处),如下所示:
return collection.stream().anyMatch(method::isAnnotationPresent);
Basically, you are providing the isAnnotationPresent()
methoddefinitionto the Lambda expression(of anyMatch
method which accepts for Predicate) and the value from the stream will automatically be passed as an argument to the anyMatch
method.
基本上,您正在向Lambda 表达式(接受Predicate的方法)提供isAnnotationPresent()
方法定义,并且流中的值将自动作为参数传递给该方法。anyMatch
anyMatch