我正在尝试使用流将 a 转换List为Map没有重复项,但我无法实现。
我可以使用这样的简单循环来做到这一点:
List<PropertyOwnerCommunityAddress> propertyOwnerCommunityAddresses = getPropertyOwnerAsList();
Map<Community, List<Address>> hashMap = new LinkedHashMap<>();
for (PropertyOwnerCommunityAddress poco : propertyOwnerCommunityAddresses) {
if (!hashMap.containsKey(poco.getCommunity())) {
List<Address> list = new ArrayList<>();
list.add(poco.getAddress());
hashMap.put(poco.getCommunity(), list);
} else {
hashMap.get(poco.getCommunity()).add(poco.getAddress());
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用流时,我的思绪崩溃了。
我不得不说PropertyOwnerCommunityAddress更多包含两个对象:Community并且Address所有这一切的目标是为每个社区保存一key:value对地址而不重复Community对象。
任何人都可以帮助我吗?谢谢!