Digital
Digital

Reputation: 591

Iterate two lists to find a match and return boolean using java8

I have two lists as below

List<String> pathNames = Lists.newArrayList("/state", "/country", "/country/name");
List<String> newPaths = Lists.newArrayList("/country/name");

I have normal for each java code to iterate two lists to find a match and return boolean as below

if(CollectionUtils.isNotEmpty(pathNames)) {
    for (String path : newPaths) {
        for (String reqPath: pathNames) {
            if(FilenameUtils.wildcardMatch(path, reqPath)) {
               return true;
            }
        }
    }
}

But I want to refactor and do the same in Java8, any help can be much appreciated.

Upvotes: 1

Views: 747

Answers (1)

ernest_k
ernest_k

Reputation: 45319

You can use a stream pipeline on both lists and call anyMatch:

return CollectionUtils.isNotEmpty(pathNames) &&
       newPaths.stream().anyMatch(np -> 
          pathNames.stream().anyMatch(pn -> FilenameUtils.wildcardMatch(np, pn)));

Upvotes: 3

Related Questions