I want to create a FileNameFilter using a lambda function.
My main problem is that I want to introduce a loop inside the function. I tried using filters but I am unable to create filters based on external list.
Following is my code:
File targetDir = new File(path);
FilenameFilter deleteFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
for(String tag: pageTagsList){
if(name.equals(tag))
return true;
}
return false;
}
};
File[] foundFiles = targetDir.listFiles(deleteFilter);
What I want to do is something like this:
File[] foundFiles = targetDir.listFiles(
(dir,name)->pageTagsList.stream()
.filter(//code to filter here)
.orElse(false)
);
Is there a correct way to do this?
pageTagsSetof typeSet<String>. Looking up in aHashSetis way faster than iterating over a list. So(dir,name)->pageTagsList.contains(name)would do it