How to use Method reference for FileFilter with getName().endsWith()

212 views Asked by At

I have a lambda expression of type FileFilter that returns true for all files ending with .java -

FileFilter lambdaFilter = (File pathname) -> pathname.getName().endsWith(".java");

I want to use Method reference for above expression. My unsuccessful attempt -

FileFilter lambdaFilter = File::getName.endsWith(".java");

Is it possible to use Method Reference for above case? How?

1

There are 1 answers

0
Horatiu Jeflea On

You can do chaining this way

.map(File::getName).map(name -> name.endsWith(".java"))

But not in a single operation