英文:
Convert list of payloads with a property List<String> to HashMap<String, Payload)
问题
I've a list of payloads and a payload looks like
Payload {
public int id;
public String name;
public String foo;
public List<String> list;
}
now I want this to a HashMap where the key is a value of the list
property, like
payloads = [
{id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]},
{id:2, name:"John", foo:"various", list:["CCC", "DDD"]},
...
];
to HashMap<String, Payload>
map = [
{key: "AAA", value: {id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]}},
{key: "BBB", value: {id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]}},
{key: "CCC", value: {id:2, name:"John", foo:"various", list:["CCC", "DDD"]}},
{key: "DDD", value: {id:2, name:"John", foo:"various", list:["CCC", "DDD"]}},
]
I've tried .flatMap()
and own mapping collectors but nothing worked. If I research I only find solutions to group by flat payload objects, like Payload.name nothing if the group key is in a List of these objects.
Any ideas?
英文:
I've a list of payloads and a payload looks like
Payload {
public int id;
public String name;
public String foo;
public List<String> list;
}
now I want this to a HashMap where the key is a value of the list
property, like
payloads = [
{id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]},
{id:2, name:"John", foo:"various", list:["CCC", "DDD"]},
...
];
to HashMap<String, Payload>
map = [
{key: "AAA", value: {id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]},
{key: "BBB", value: {id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]},
{key: "CCC", value: {id:2, name:"John", foo:"various", list:["CCC", "DDD"]},,
{key: "DDD", value: {id:2, name:"John", foo:"various", list:["CCC", "DDD"]},,
]
I've tried .flatMap()
and own mapping collectors but nothing worked. If I research I only find solutions to group by flat payload objects, like Payload.name nothing if the group key is in a List of these objects.
Any ideas?
答案1
得分: 1
在这种情况下,我可能会选择简单的命令式方法:
Map<String, Payload> map = new HashMap<>();
for (Payload payload : list) {
for (String value : payload.list) {
map.put(value, payload); // 你可能需要检查这是否返回 null
}
}
return map;
英文:
In this case, I'd probably just go for the simple, imperative approach:
Map<String, Payload> map = new HashMap<>();
for (Payload payload : list) {
for (String value : payload.list) {
map.put(value, payload); // you may want to check this returns null
}
}
return map;
答案2
得分: 1
假设给定 List<Payload> payloads
,可以使用流API来实现如下:
payloads.stream().flatMap(payload ->
payload.list.stream().map(listValue -> Map.entry(listValue, payload))
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
在Java 8中使用 new AbstractMap.SimpleEntry()
替代 Map.entry()
。
然而,也许值得使用 @Jorn 的方法。
英文:
Assume given List<Payload> payloads
with stream API it could be done as follows:
payloads.stream().flatMap(payload ->
payload.list.stream().map(listValue -> Map.entry(listValue, payload))
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
With Java 8 use new AbstractMap.SimpleEntry()
instead of Map.entry()
Although, it may be worthy to use @Jorn approach
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论