I have an object called UserSettings which contains a list of another object called SettingsFolder. This object in-turn has a List of SettingsFolders. The idea is to mimic a user that has a set of folders and folders can have subfolders. I am trying to write a lambda function which will find a given object provided the given ID.
Right now I have 2 lambdas, one which converts the top-level folder into streams and checks if the Id exists and returns the object and another which checks the sub-folder.
For parent Level folder
SettingsFolder settingsFolder = userSettings.getFolders().stream()
.filter(folder -> folder.getId().equals(UUID.fromString(form.getFolderId())))
.findAny()
.orElse(null);
For SubFolder
List<SettingsFolder> settingsFolders = userSettings.getFolders().stream()
.filter(folder -> folder.getFolders() != null)
.filter(folder -> folder.getFolders().size() > 0)
.collect(Collectors.toList());
SettingsFolder subFolder = settingsFolders.stream()
.flatMap(e -> e.getFolders().stream())
.filter(c -> c.getId() != null)
.filter(c -> c.getId().equals(UUID.fromString(form.getFolderId())))
.findAny()
.orElse(null);
Is there a way to combine 2 lambdas into one which will ideally check all these lists recursively?