I'm trying to code a multilevel filter. I have folders and files.
public class Folder {
String name;
List<Folder> folders;
List<File> files;
...
}
public class File {
String name;
String type;
...
}
I need a filter who looks for some property e.g. name, which can be a folder/file name. I wrote a piece of code that works for one level, but I don't how could I do that looking for two level loops.
The final result would be a list of folders: the ones which names match the 'name filter' or folders which file names match the filter.
List<Folder> result = folders.stream()
.filter(folder -> folder.getFiles().stream().anyMatch(file -> file.getName().contains(filter)))
.collect(Collectors.toList());