First things first, your formatting is horrible, sort it out!
Now, lambda syntax; to convert the anonymous class:
final FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return false;
}
};
We start by replacing the anonymous class with an equivalent lambda for the single method accept(File dir, String name)
:
final FilenameFilter filter = (File dir, String name) -> {
return false;
};
But we can do better, we don't need to define the types - the compiler can work those out:
final FilenameFilter filter = (dir, name) -> {
return false;
};
And we can do better still, as the method return a boolean
; if we have a single statement that evaluates to a boolean
we can skip the return
and the braces:
final FilenameFilter filter = (dir, name) -> false;
This can be any statement, for example:
final FilenameFilter filter = (dir, name) -> !dir.isDirectory() && name.toLowerCase().endsWith(".txt");
However, the File
API is very old, so don't use it. Use the nio API
. This has been around since Java 7 in 2011 so there is really no excuse:
final Path p = Paths.get("/", "home", "text", "xyz.txt");
final DirectoryStream.Filter<Path> f = path -> false;
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(p, f)) {
stream.forEach(System.out::println);
}
And in fact your example has a specific method built into Files
that takes a Glob:
final Path p = Paths.get("/", "home", "text", "xyz.txt");
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(p, "*.txt")) {
stream.forEach(System.out::println);
}
Or, using the more modern Files.list
:
final Path p = Paths.get("/", "home", "text", "xyz.txt");
final PathMatcher filter = p.getFileSystem().getPathMatcher("glob:*.txt");
try (final Stream<Path> stream = Files.list(p)) {
stream.filter(filter::matches)
.forEach(System.out::println);
}
Here filter::matches
is a method reference because the method PathMatcher.matches
can be used to implement the functional interface Predicate<Path>
as it takes a Path
and returns a boolean
.
As an aside:
f.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
name.endsWith(".txt");
return false;
}
});
This makes no sense...