Java 8的groupingBy,用于获取LinkedHashMap,并将映射的值映射到不同的对象

huangapple go评论67阅读模式
英文:

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&lt;String, List&lt;ResourceManagementDTO&gt;&gt; getAccountsByGroupNameMap(final List&lt;AccountManagement&gt; accountManagementList) {
    
    return new LinkedHashMap&lt;&gt;(accountManagementList.stream().collect(Collectors.groupingBy(acc -&gt; 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&lt;String, List&lt;AccountManagement&gt;&gt; getAccountsByGroupNameMap(final List&lt;AccountManagement&gt; accountManagementList) {
    return accountManagementList.stream()
                                 .collect(groupingBy(acc -&gt; 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&lt;String, List&lt;ResourceManagementDTO&gt;&gt;
			getAccountsByGroupNameMap(
					final List&lt;AccountManagement&gt; accountManagementList) {
		
		return accountManagementList.stream()
				.collect(Collectors.groupingBy(
						acc -&gt; acc.getGroup().getName(),
						LinkedHashMap::new,
						Collectors.mapping(
								ResourceManagementDTOMapper::toResourceManagementDTO,
								Collectors.toList())));

</details>



huangapple
  • 本文由 发表于 2020年9月4日 22:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63742736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定