我是Java 8的新手,并试图理解这些概念。我正在
Missing return type
我传递给ReadTheFile
方法的Lambda表达式的错误。
package java8.programs;
import java.io.BufferedReader;
import java.io.IOException;
@FunctionalInterface
public interface BufferReadderProcessor {
String process(BufferedReader br) throws IOException;
}
package java8.programs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class MainJava {
public static void main(String[] args) throws IOException {
MainJava obj = new MainJava();
String message = obj.ReadTheFIle((BufferedReader br) -> {
while (br.readLine()!=null)
br.readLine();
});
System.out.println(message);
}
public String ReadTheFIle(BufferReadderProcessor bp) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("G:\\DemoJavaFile.txt")));
return bp.process(br);
}
}
答案 0 :(得分:4)
您的lambda表达式实现了BufferReaderProcessor
的{{1}}方法,因此它应返回String process(BufferedReader br)
。
例如,假设您希望返回从String
:
BufferedReader