我对“如何正确地做这件事”有点困惑:
// return true: if present and number of lines != 0
boolean isValid(Optional<File> optFile) {
return optFile.ifPresentOrElse(f -> return !isZeroLine(f), return false);
}
private boolean isZeroLine(File f) {
return MyFileUtils.getNbLinesByFile(f) == 0;
}
我知道语法不正确,不能编译,但这只是你的想法。
我怎么才能把它变成“干净的代码”呢?例如,避免做以下事情:
if (optFile.isPresent()) {//} else {//}
https://stackoverflow.com/questions/52617309
复制相似问题