FilenameFilter的java 8 lambda表达式 [英] java 8 lambda expression for FilenameFilter

查看:1628
本文介绍了FilenameFilter的java 8 lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览java 8中的lambda表达式

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();

转换为lambda表达式为

is converted to lambda expression as

new Thread(
    () -> System.out.println("Hello from thread")
).start();

但我无法转换FilenameFilter表达式

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;
    }
});

并且未成功转换为

file.list(new FilenameFilter () {
    (File a1, String a2) -> { 
        return false;
    }
});

它在eclipse中给出错误

it's giving error as in eclipse as


此行的多个标记

- 语法错误,插入;完成语句

- 语法错误,插入}以完成阻止

- 语法错误,插入AssignmentOperator表达式以完成作业

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!

现在,lambda语法;转换匿名类:

Now, lambda syntax; to convert the anonymous class:

final FilenameFilter filter = new FilenameFilter() {
    @Override
    public boolean accept(File dir, String name) {
        return false;
    }
};

我们首先用单一方法的等效lambda替换匿名类 accept(文件目录,字符串名称)

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;
};

但我们可以做得更好,我们不需要定义类型 - 编译器可以工作那些out:

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;
};

我们可以做得更好,因为该方法返回布尔值;如果我们有一个评估为 boolean 的语句,我们可以跳过 return 和大括号:

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");

然而,文件 API是非常旧,所以不要使用它。使用 nio API 。自2011年Java 7以来一直存在,所以确实有没有借口:

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);
}

事实上你的例子中有一个内置于文件 接受一个Glob

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);
}

或者,使用更现代的 Files.list

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);
}

这里 filter :: matches 是方法引用,因为方法 PathMatcher.matches 可用于实现功能接口 Predicate< Path> 因为它需要路径并返回布尔值

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.

暂且不说:

f.list(new FilenameFilter() {

    @Override
    public boolean accept(File dir, String name) {
        name.endsWith(".txt");
        return false;
    }
});

这没有任何意义......

This makes no sense...

这篇关于FilenameFilter的java 8 lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆