Is it possible to go back to the parent-object after applying flatmap() operation, and accumulate parent-objects into a set?
I have an UnifiedOfferEntity with set of other entity objects as a field:
public static class UnifiedOfferEntity { private Set<ContractDetailsEntity> contractDetails; // getters, etc. } I would like to filter through fields of the parent-object (UnifiedOfferEntity) like this:
offersFromDB.stream() .filter(offer -> CollectionUtils.containsAny(preferences.getOwnedSkills(), offer.getSkills()) && CollectionUtils.containsAny(preferences.getOwnedSeniority(), offer.getSeniority())) And then I would like to examine the nested collection, filter through child-objects (ContractDetailsEntity):
.flatMap(offer -> offer.getContractDetails().stream() .filter(cd -> cd.getSalaryFrom() >= preferences.getSalaryMin()) And finally, I need to move back to the parent-object and collect its instances into a Set after these filters.
I was trying with this:
List<UnifiedOfferEntity> offersFromDB = // initializing somehow Set<UnifiedOfferEntity> result = offersFromDB.stream() .filter(offer -> CollectionUtils.containsAny(preferences.getOwnedSkills(), offer.getSkills()) && CollectionUtils.containsAny(preferences.getOwnedSeniority(), offer.getSeniority())) .flatMap(offer -> offer.getContractDetails().stream() .filter(cd -> cd.getSalaryFrom() >= preferences.getSalaryMin() && cd.getSalaryTo() <= preferences.getSalaryMax() && tocPreferences.contains(cd.getTypeOfContract()))) .collect(Collectors.toSet()) But it creates a Set of ContractDetailsEntity, not UnifiedOfferEntity. How can I fix this?