You can collect the result after the filter operation into an instance of the list, and then check the size before it works.
List<Path> resultSet = Files.walk(rootDir) .filter(matcher::matches) .collect(Collectors.toList()); if(resultSet.size() > 0){ resultSet.forEach(Start::modify); }else {
Alternatively, you can do something like this:
if(Files.walk(rootDir).anyMatch(matcher::matches)) { Files.walk(rootDir) .filter(matcher::matches) .forEach(Start::modify); }else { // do something else }
source share