I have the following entities (unnecessary parts are omitted for brevity):
public class Site {
private UUID uuid;
private UUID countryUuid;
}
public class Country {
private UUID uuid;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String code;
@Column(nullable = false)
private Currency currency;
}
I have List<UUID>
of Site
and try to create Map<UUID, Currency>
for siteUuid
and Currency
pair. I have tried something as shown below in order to go by using country list but have not managed.
// get site list
final List<SiteDTO> siteList = siteService.findAllByUuidIn(siteUuidList);
// then get country list
final List<CountryDTO> countryList = siteList.stream()
.map(SiteDTO::getCountry).collect(Collectors.toList());
// then finall try to map them to Currency
final Map<UUID, CountryDTO> uuidCurrencyMap = countryList
.stream().collect(Collectors.toMap(CountryDTO::getUuid, Function.identity()));
However, uuidCurrencyMap
gives countryUuid
- Currency
pairs, but I need siteUuid
- Currency
pairs. So, is it possible?
The following should work:
List<SiteDTO> siteList = siteService.findAllByUuidIn(siteUuidList);
Map<UUID, Currency> siteCurrency = siteList.stream()
.collect(Collectors.toMap(SiteDTO::getUuid, siteDTO -> siteDTO.getCountry().getCurrency()));