将具有属性List的有效负载列表转换为HashMap

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

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&lt;String&gt; list;
}

now I want this to a HashMap where the key is a value of the list property, like

payloads = [
 {id:45, name:&quot;Alfred&quot;, foo:&quot;bar&quot;, list:[&quot;AAA&quot;, &quot;BBB&quot;]},
 {id:2, name:&quot;John&quot;, foo:&quot;various&quot;, list:[&quot;CCC&quot;, &quot;DDD&quot;]},
 ...
];

to HashMap<String, Payload>

map = [
{key: &quot;AAA&quot;, value: {id:45, name:&quot;Alfred&quot;, foo:&quot;bar&quot;, list:[&quot;AAA&quot;, &quot;BBB&quot;]},
{key: &quot;BBB&quot;, value: {id:45, name:&quot;Alfred&quot;, foo:&quot;bar&quot;, list:[&quot;AAA&quot;, &quot;BBB&quot;]},
{key: &quot;CCC&quot;, value: {id:2, name:&quot;John&quot;, foo:&quot;various&quot;, list:[&quot;CCC&quot;, &quot;DDD&quot;]},,
{key: &quot;DDD&quot;, value: {id:2, name:&quot;John&quot;, foo:&quot;various&quot;, list:[&quot;CCC&quot;, &quot;DDD&quot;]},,
]

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&lt;String, Payload&gt; map = new HashMap&lt;&gt;();
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&lt;Payload&gt; 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&lt;Payload&gt; payloads with stream API it could be done as follows:

    payloads.stream().flatMap(payload -&gt;
            payload.list.stream().map(listValue -&gt; 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

huangapple
  • 本文由 发表于 2023年6月8日 17:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430253.html
匿名

发表评论

匿名网友

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

确定