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

你可能感兴趣的:(java,lambda)