英文:
How to flatten a map inside a list?
问题
import java.util.*;
import java.util.stream.Collectors;
List<Map<String, Object>> source = List.of(
Map.of("type", "first_type", "value", "1"),
Map.of("type", "first_type", "value", "2"),
Map.of("type", "second_type", "value", "3")
);
Map<String, List<Object>> goal = source.stream()
.collect(Collectors.groupingBy(
e -> (String) e.get("type"),
Collectors.mapping(e -> e.get("value"), Collectors.toList())
));
英文:
I have a list of maps
, each map showing the same key attributes type
and value
.
Question: how can I use java streams to flatten this into a Map<String, List>>
?
List<Map<String, Object>> source = List.of(
Map.of("type", "first_type", "value", "1"),
Map.of("type", "first_type", "value", "2"),
Map.of("type", "second_type", "value", "3")
);
Goal:
Map<String, List<Object>> goal = Map.of(
"first_type", List.of("1", "2"),
"second_type", List.of("3")
);
It's probably similar to the following, but how can I group the nested map values by their type?
source.stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
答案1
得分: 4
我们可以使用Collectors.groupingBy
来实现这一点。
Map<String, List<Object>> goal = source.stream()
.collect(Collectors.groupingBy(e -> (String) e.get("type"),
Collectors.mapping(e -> e.get("value"), Collectors.toList())));
由于结果映射的键是字符串,因此在提取键(type
)时需要进行丑陋的类型转换。
英文:
We can achieve this using Collectors.groupingBy
.
Map<String, List<Object>> goal = source.stream()
.collect(Collectors.groupingBy(e -> (String) e.get("type"),
Collectors.mapping(e -> e.get("value"), Collectors.toList())));
Since the key of the result map is a String, there is an ugly type-casting when extracting the key (type
).
答案2
得分: 2
你可以使用 Collectors.groupingBy
按 type
进行分组,然后使用 Collectors.mapping
映射 value
并收集为列表:
Map<Object, List<Object>> goal =
source.stream()
.collect(Collectors.groupingBy(m -> m.get("type"),
Collectors.mapping(m -> m.get("value"), Collectors.toList())));
最好将结果保存在 Map<Object, List<Object>>
中,以使映射的键为 Object
,避免进行类型转换。
英文:
You can use Collectors.groupingBy
to grouping by type
and Collectors.mapping
to map the value
and collect as List
Map<Object, List<Object>> goal =
source.stream()
.collect(Collectors.groupingBy(m -> m.get("type"),
Collectors.mapping(m -> m.get("value"), Collectors.toList())));
It's better get result in Map<Object, List<Object>>
as map key is Object
to avoid type casting.
答案3
得分: 0
只需将地图流式传输并提取类型和值,然后将它们映射到列表中。
这使用groupingBy
将值分组到目标供应商中。但是您需要先映射该值,然后将供应商指定为List
。我还将Object
更改为String
,因为那是您正在使用的内容。
Map<String, List<String>> map =
source.stream()
.collect(Collectors.groupingBy(m -> m.get("type"),
Collectors.mapping(m -> m.get("value"),
Collectors.toList())));
map.entrySet().forEach(System.out::println);
打印结果为
first_type=[1, 2]
second_type=[3]
当然,您也可以这样做。这将被翻译为:
- 如果指定的键
type
对应的ArrayList存在,请使用它。 - 否则创建它。
- 在任一情况下,它都会从
compute
方法返回该列表,其中添加了value
键的值。
Map<String, List<String>> map = new HashMap<>();
for (Map<String, String> m : source) {
map.compute(m.get("type"), (k, v) -> v == null ?
new ArrayList<>() : v).add(m.get("value"));
}
英文:
Just Stream the maps and pull off the type and values. Then map them to a list.
This uses groupingBy
to group the values into a target supplier. But you need to map the value first and then specify the supplier as a List
. I also changed Object
to String
since that is what you are using.
Map<String, List<String>> map =
source.stream()
.collect(Collectors.groupingBy(m->m.get("type"),
Collectors.mapping(m->m.get("value"),
Collectors.toList())));
map.entrySet().forEach(System.out::println);
Prints
first_type=[1, 2]
second_type=[3]
Of course, you could also do it like this. This translates to:
- if the ArrayList is there for the specified key
type
, use it. - otherwise create it.
- In either case it returns that list from the
compute
method where<br>
the value for thevalue
key is added.
Map<String,List<String>> map = new HashMap<>();
for(Map<String,String> m : source) {
map.compute(m.get("type"), (k,v)-> v == null ?
new ArrayList<>(): v).add(m.get("value"));
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论