filenamefilter java,FilenameFilter的java 8 lambda表达式

这篇博客介绍了如何将 Java 8 中的传统匿名类转换为 Lambda 表达式,特别是在处理 FilenameFilter 的情况下。文章通过实例展示了如何从匿名类的 accept 方法转换为简洁的 Lambda 表达式,并强调了使用 Java NIO API 替代过时的 File API 的重要性。此外,还提供了关于错误修正和优化 Lambda 表达式的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am going through the lambda expression in java 8

when i changed the code of thread it's working fine

new Thread(new Runnable() {

@Override

public void run() {

System.out.println("run");

}

}).start();

is converted to lambda expression as

new Thread(

() -> System.out.println("Hello from thread")

).start();

But i am not able to convert the FilenameFilter Expression

File file = new File("/home/text/xyz.txt");

file.list(new FilenameFilter() {

@Override

public boolean accept(File dir, String name) {

name.endsWith(".txt");

return false;

}

});

and unsuccessfully converted to this as

file.list(new FilenameFilter () {

(File a1, String a2) -> {

return false;

}

});

it's giving error as in eclipse as

Multiple markers at this line

- Syntax error, insert ";" to complete Statement

- Syntax error, insert "}" to complete Block

- Syntax error, insert "AssignmentOperator Expression" to complete Assignment

解决方案

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 f = path -> false;

try (final DirectoryStream 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 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 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 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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值