将列表的列表使用流合并为映射

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

Combine list of list into map using stream

问题

import java.util.*;
import java.util.stream.Collectors;

class Pojo {
    String type;
    List<String> ids;
}

public class Main {
    public static void main(String[] args) {
        List<Pojo> pojoList = new ArrayList<>();
        // Add your Pojo objects to the list
        
        Map<String, List<String>> resultMap = pojoList.stream()
                .flatMap(pojo -> pojo.ids.stream().map(id -> new AbstractMap.SimpleEntry<>(id, pojo.type)))
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                        Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
        
        System.out.println(resultMap);
    }
}

This code uses Java streams to achieve the desired conversion. Replace the comment with your actual list of Pojo objects. The code processes each Pojo object, extracts the type and ids, and then creates pairs of id-type mappings. These pairs are collected into a map where the keys are the ids and the values are lists of associated types.

英文:

I'm trying to convert a pojo object that looks like

[
  {
    &quot;type&quot;: &quot;Preferred&quot;,
    &quot;ids&quot;: [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;]
   },
  {
    &quot;type&quot;: &quot;Non-preferred&quot;,
    &quot;ids&quot;: [&quot;D&quot;, &quot;E&quot;]
  },
  {
    &quot;type&quot;: &quot;Popular&quot;,
    &quot;ids&quot;: [&quot;A&quot;, &quot;D&quot;]
  }
]

into Map&lt;String, List&lt;String&gt;&gt;, such as:

{
 &quot;A&quot;: [&quot;Preferred&quot;, &quot;Popular&quot;],
 &quot;B&quot;: [&quot;Preferred&quot;],
 &quot;C&quot;: [&quot;Preferred&quot;],
 &quot;D&quot;: [&quot;Non-preferred&quot;, &quot;Popular&quot;],
 &quot;E&quot;: [&quot;Non-preferred&quot;],
}

how can I accomplish this using stream? I preferably want to utilize stream into collect(), instead of using forEach() (which is basically a for-loop).

Thanks in advance.

EDIT:
The pojo class looks something like:

class Pojo {
 String type;
 List&lt;String&gt; ids;
}

And I basically have List&lt;Pojo&gt;

答案1

得分: 0

你可以使用流(stream)来实现,为每种类型和ID组合创建条目,然后进行分组操作。

Map<String, List<String>> results = lists.stream()
        .flatMap(obj -> obj.getIds()
                .stream()
                .map(id -> new AbstractMap.SimpleEntry<>(id, obj.getType())))
        .collect(Collectors.groupingBy(Map.Entry::getKey,
                Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
英文:

You can do it using stream, create entry for each type and id combination and then do a group by

Map&lt;String, List&lt;String&gt;&gt; results = lists.stream()
            .flatMap(obj-&gt;obj.getIds()
                    .stream()
                    .map(id-&gt;new AbstractMap.SimpleEntry&lt;&gt;(id,obj.getType())))
            .collect(Collectors.groupingBy(Map.Entry::getKey,
                    Collectors.mapping(Map.Entry::getValue,Collectors.toList())));

huangapple
  • 本文由 发表于 2020年9月1日 08:40:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63679863.html
匿名

发表评论

匿名网友

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

确定