我有以下实体(为简洁起见,省略了不必要的部分):
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;
}
我有
List<UUID>
的 Site
,并尝试为 Map<UUID, Currency>
和 siteUuid
对创建 Currency
。我已经尝试了如下所示的方法,以便使用国家/地区列表,但没有成功。
// 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()));
但是,
uuidCurrencyMap
给出 countryUuid
- Currency
对,但我需要 siteUuid
- Currency
对。那么,可以吗?
以下应该有效:
List<SiteDTO> siteList = siteService.findAllByUuidIn(siteUuidList);
Map<UUID, Currency> siteCurrency = siteList.stream()
.collect(Collectors.toMap(SiteDTO::getUuid, siteDTO -> siteDTO.getCountry().getCurrency()));