lambda表达式的Java 8“缺少返回类型”错误

时间:2016-09-13 11:00:35

标签: java lambda java-8

我是Java 8的新手,并试图理解这些概念。我正在

Missing return type
我传递给ReadTheFile方法的Lambda表达式的

错误。

BufferReaderProcessor

package java8.programs;

import java.io.BufferedReader;
import java.io.IOException;

@FunctionalInterface
public interface BufferReadderProcessor {
    String process(BufferedReader br) throws IOException;
}

MainJava

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

1 个答案:

答案 0 :(得分:4)

您的lambda表达式实现了BufferReaderProcessor的{​​{1}}方法,因此它应返回String process(BufferedReader br)

例如,假设您希望返回从String

中读取的数据
BufferedReader