英文:
Java 8 groupingBy to obtain LinkedHashMap and mapping the values of the map to a different object
问题
我有这个返回Map的方法:
public Map<String, List<ResourceManagementDTO>> getAccountsByGroupNameMap(final List<AccountManagement> accountManagementList) {
return new LinkedHashMap<>(accountManagementList.stream().collect(Collectors.groupingBy(acc -> acc.getGroup().getName(),
Collectors.mapping(ResourceManagementDTOMapper::toResourceManagementDTO, Collectors.toList()))));
}
我需要我的Map是一个LinkedHashMap,但是上面的代码似乎不起作用,因为键的顺序没有保留。我设法找到了另一种方法,可以返回一个LinkedHashMap,但是使用那种语法时,我无法再执行映射操作(将AccountManagement映射到ResourceManagementDTO)。下面是代码:
public Map<String, List<AccountManagement>> getAccountsByGroupNameMap(final List<AccountManagement> accountManagementList) {
return accountManagementList.stream()
.collect(groupingBy(acc -> acc.getGroup().getName(), LinkedHashMap::new, Collectors.toList()));
}
有没有办法在单个Java 8流水线中获得LinkedHashMap,并执行映射操作?我真的无法找到将这两个操作结合起来的语法。
英文:
I have this method which returns a Map:
public Map<String, List<ResourceManagementDTO>> getAccountsByGroupNameMap(final List<AccountManagement> accountManagementList) {
return new LinkedHashMap<>(accountManagementList.stream().collect(Collectors.groupingBy(acc -> acc.getGroup().getName(),
Collectors.mapping(ResourceManagementDTOMapper::toResourceManagementDTO, Collectors.toList()))));
}
I need my map to be a LinkedHaspMap, but the above code doesn't seem to work, because the order of the keys isn't preserved. I managed to find another approach which would return a LinkedHashMap, however with that syntax I wasn't able to do the mapping operation anymore (to map AccountManagement to ResourceManagementDTO). Here's the code:
public Map<String, List<AccountManagement>> getAccountsByGroupNameMap(final List<AccountManagement> accountManagementList) {
return accountManagementList.stream()
.collect(groupingBy(acc -> acc.getGroup().getName(), LinkedHashMap::new, Collectors.toList()));
}
Is there a way to get the LinkedHashMap and also perform the mapping operation in a single Java 8 pipeline? I really couldn't figure an syntax that combines both operations.
答案1
得分: 1
尝试以下操作:groupingBy 使用映射类型的供应商。
public Map<String, List<ResourceManagementDTO>>
getAccountsByGroupNameMap(
final List<AccountManagement> accountManagementList) {
return accountManagementList.stream()
.collect(Collectors.groupingBy(
acc -> acc.getGroup().getName(),
LinkedHashMap::new,
Collectors.mapping(
ResourceManagementDTOMapper::toResourceManagementDTO,
Collectors.toList())));
英文:
Try the following: groupingBy takes a supplier for the map type.
public Map<String, List<ResourceManagementDTO>>
getAccountsByGroupNameMap(
final List<AccountManagement> accountManagementList) {
return accountManagementList.stream()
.collect(Collectors.groupingBy(
acc -> acc.getGroup().getName(),
LinkedHashMap::new,
Collectors.mapping(
ResourceManagementDTOMapper::toResourceManagementDTO,
Collectors.toList())));
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论