关于java:java-lamda表达式

@FunctionalInterface
interface FileHandle {
    void doSome(String fileContent);
}
public class LamdaTest {
    //能够了解为一种匿名函数的代替
    //可选参数类型 
    //合乎lamda表达式的函数时接口:只有一个形象办法 
    //函数式接口注解@FunctionInterface 非必要
    //函数式接口的形象办法签名:函数描述符 
    public static void handleFileContent(String url,FileHandle fileHandle) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(url));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = bufferedReader.readLine())!= null) {
            stringBuilder.append(line + '\n');
        }
        fileHandle.doSome(stringBuilder.toString());
    }
    public static void main(String[] args) throws IOException {
        LamdaTest.handleFileContent("D:\\hello.txt",str -> System.out.println(str.toUpperCase()));
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理