@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()));
}
}
- util下function中有不少jdk自帶的FunctionInterface,進一步泛化,能夠選擇使用
方法引用