简体   繁体   English

Java 8 Method表达式给出了编译错误

[英]Java 8 Method expression is giving compilation error

I am new to Java 8 (Lambda Expression). 我是Java 8(Lambda Expression)的新手。 The following piece of code prints all the directories at the path indicated by the variable "file". 下面的代码打印变量“file”指示的路径上的所有目录。

File file = new File("/vssexclude/Test/workspace/Test");

File[] names = file.listFiles(fileName -> fileName.isDirectory());

for (File name : names) {
    System.out.println(name.toString());
}

But, when I try to replace the lambda expression with method expression, eclipse is giving compilation error: 但是,当我尝试用方法表达式替换lambda表达式时,eclipse会给出编译错误:

File[] names = file.listFiles(File::isDirectory());

What am I missing? 我错过了什么?

Remove the parenthesis : 删除括号:

File[] names = file.listFiles(File::isDirectory);

When you want to refer to an instance method of an object of a particular type, the syntax to use is : 如果要引用特定类型对象的实例方法,则使用的语法是:

ContainingType::methodName

This is described in more details here . 这里将更详细地描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM