menteith
menteith

Reputation: 678

Else clause in lambda expression

I use the following lambda expression to iterate over PDF files.

public static void run(String arg) {

        Path rootDir = Paths.get(arg);
        PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.pdf");
        Files.walk(rootDir)
                .filter(matcher::matches)
                .forEach(Start::modify);
    }

    private static void modify(Path p) {
        System.out.println(p.toString());
    }

This part .forEach(Start::modify); executes the static method modify from the same class where the lambda expression is located. Is there a possibility to add something like else clause when no PDF file is found?

Upvotes: 6

Views: 224

Answers (3)

Paul Boddington
Paul Boddington

Reputation: 37645

If an API gives you a Stream, but Stream isn't what you need, you can always convert it to an Iterable and use a simple for loop:

boolean fileModified = false;
for (Path path : (Iterable<Path>) Files.walk(rootDir)::iterator) {
    if (matcher.matches(path)) {
        Start.modify(path);
        fileModified = true;
    }
} 
if (!fileModified) {
     // do something
}

This iterates the files only once and doesn't require forming an intermediate collection.

Upvotes: 1

Brian Goetz
Brian Goetz

Reputation: 95376

Or you could just do the obvious thing, which is collect the stream first.

List<File> files = Files.walk(rootDir)
            .filter(matcher::matches)
            .collect(toList());

if (files.isEmpty())
    doSomethingForEmpty();
else
    files.forEach(Start::modify);

Upvotes: 6

Ousmane D.
Ousmane D.

Reputation: 56433

You could collect the result after the filter operation into a list instance and then check the size before operating on it.

List<Path> resultSet = Files.walk(rootDir)
                            .filter(matcher::matches)
                            .collect(Collectors.toList());
if(resultSet.size() > 0){
    resultSet.forEach(Start::modify);
}else {
    // do something else   
}

Alternatively, you could do something like this:

if(Files.walk(rootDir).anyMatch(matcher::matches)) {
         Files.walk(rootDir)
              .filter(matcher::matches)
              .forEach(Start::modify);
}else {
        // do something else    
}

Upvotes: 2

Related Questions