Can be replaced with method reference using reflection in java

Motomine :

I have this code in 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.

Does anyone know how to make that?

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() method definition to 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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=443298&siteId=1