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

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

Convert list of payloads with a property List<String> to HashMap<String, Payload)

问题

I've a list of payloads and a payload looks like

  1. Payload {
  2. public int id;
  3. public String name;
  4. public String foo;
  5. public List<String> list;
  6. }

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

  1. payloads = [
  2. {id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]},
  3. {id:2, name:"John", foo:"various", list:["CCC", "DDD"]},
  4. ...
  5. ];

to HashMap<String, Payload>

  1. map = [
  2. {key: "AAA", value: {id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]}},
  3. {key: "BBB", value: {id:45, name:"Alfred", foo:"bar", list:["AAA", "BBB"]}},
  4. {key: "CCC", value: {id:2, name:"John", foo:"various", list:["CCC", "DDD"]}},
  5. {key: "DDD", value: {id:2, name:"John", foo:"various", list:["CCC", "DDD"]}},
  6. ]

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

  1. Payload {
  2. public int id;
  3. public String name;
  4. public String foo;
  5. public List&lt;String&gt; list;
  6. }

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

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

to HashMap<String, Payload>

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

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

在这种情况下,我可能会选择简单的命令式方法:

  1. Map<String, Payload> map = new HashMap<>();
  2. for (Payload payload : list) {
  3. for (String value : payload.list) {
  4. map.put(value, payload); // 你可能需要检查这是否返回 null
  5. }
  6. }
  7. return map;
英文:

In this case, I'd probably just go for the simple, imperative approach:

  1. Map&lt;String, Payload&gt; map = new HashMap&lt;&gt;();
  2. for (Payload payload : list) {
  3. for (String value : payload.list) {
  4. map.put(value, payload); // you may want to check this returns null
  5. }
  6. }
  7. return map;

答案2

得分: 1

假设给定 List&lt;Payload&gt; payloads,可以使用流API来实现如下:

  1. payloads.stream().flatMap(payload ->
  2. payload.list.stream().map(listValue -> Map.entry(listValue, payload))
  3. ).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:

  1. payloads.stream().flatMap(payload -&gt;
  2. payload.list.stream().map(listValue -&gt; Map.entry(listValue, payload))
  3. ).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:

确定