简体   繁体   English

将List的字段映射到另一个List

[英]Map fields of a List to another List

I am new to Java 8, I have 2 List of Chat, i wish to set some fields in List A from List B if their id is match 我是新来的Java 8,我有2个List聊天的,我想设置在某些领域List AList B ,如果他们的ID是匹配

Chat: 聊天:

public class Chat {

    private String id;

    private Status status;

    private Rating rating;

    //Setters and getters
}

I can do it by using nested loop, but not sure how to do it in java 8: 我可以通过使用嵌套循环来完成它,但不知道如何在java 8中执行它:

List<Chat> listA = getDataForA();
List<Chat> listB = getDataForB();

listA .forEach(a -> {
   listB.forEach(b-> {
       if (b.getId().equals(a.getId())) {
            a.setRating(b.getRating());
            a.setStatus(b.getStatus());
       }
  });
});

can you try this stream, from listA stream we are setting rating and status only if listA and listB ids are matching 你可以尝试这个流吗,只有当listA和listB id匹配时,我们才会从listA流设置评级和状态

    listA.stream()
            .flatMap(a -> listB.stream().filter(b -> b.getId().equals(a.getId())).map(b -> {
                a.setRating(b.getRating());
                a.setStatus(b.getStatus());
                return a;
            }))
            .collect(Collectors.toList());

I would give another approach than simple two loops: group given lists by id and then loop once over all ids: 我会给出另一种方法,而不是简单的两个循环:按id给出列表,然后在所有id上循环一次:

public static void main(String... args) {
    List<Chat> listA = getDataForA();
    List<Chat> listB = getDataForB();

    Map<String, Chat> mapA = groupById(listA);
    Map<String, Chat> mapB = groupById(listB);

    mapA.entrySet().stream()
        .filter(entry -> mapB.containsKey(entry.getKey()))
        .forEach(entry -> {
            Chat a = entry.getValue();
            Chat b = mapB.get(entry.getKey());
            a.setRating(b.getRating());
            a.setStatus(b.getStatus());
        });
}

private static Map<String, Chat> groupById(Collection<Chat> data) {
    return Optional.ofNullable(data).orElse(Collections.emptyList()).stream().collect(Collectors.toMap(Chat::getId, Function.identity()));
}

如何创建列表<object>带有字段字符串和 Map <string, set<string> &gt; 从另一个列表<object2><div id="text_translate"><p> Class Object2具有标准的 getter 并具有String字段folder 、 file和version 。 它被命名为SourceInfo</p><p> List&lt;SourceInfo&gt; source包含上面提到的三个字段。</p><p> 我的目标是从List&lt;SourceInfo&gt;创建一个List&lt;Info&gt; &gt; 。</p><p> 新List的 class 为Info ,如下图所示。</p><pre> public class Info { private final String folder; private final Map&lt;String, Set&lt;String&gt;&gt; file; public static Builder builder() { return new Builder(); } public static Builder builder(Info info) { return new Builder(info); } private Info(Builder builder) { this.folder = builder.folder; this.file = builder.file; } public String getFolder() { return folder; } public Map&lt;String, Set&lt;String&gt;&gt; getFile() { return file; } // autogenerated toString, hashCode, and equals public static class Builder { private String folder; private Map&lt;String, Set&lt;String&gt;&gt; file; private Builder() {} private Builder(Info info) { this.folder = info.folder; this.file = info.file; } public Builder with(Consumer&lt;Builder&gt; consumer) { consumer.accept(this); return this; } public Builder withFolder(String folder) { this.folder = folder; return this; } public Builder withFile(Map&lt;String, Set&lt;String&gt;&gt; file) { this.file = file; return this; } public Info build() { return new Info(this); } }</pre><p> 到目前为止,我尝试的是在构建器模式中创建一个集合。</p><pre> List&lt;SourceInfo&gt; source; // error: gc overhead limit exceeded List&lt;Info&gt; infoList = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet())))).build()).collect(Collectors.toList()); Map&lt;String, Set&lt;String&gt;&gt; map = source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet()))); List&lt;Info&gt; info = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))).build()).collect(Collectors.toList());</pre><p> 所需的 output。 以下语法可能已关闭。</p><pre> // [String, Map&lt;String, Set&lt;String&gt;&gt;] Info [folder, [key=file [value=version]]]...</pre><p> 我是 Java 的新手,不胜感激。</p><p> 我想了解如何使用 java8 和 for 循环来做到这一点。</p><p> 谢谢你。</p></div></object2></string,></object> - How to create a List<Object> with fields String and Map<String, Set<String>> from another List<Object2>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Map 用映射结构列出的几个字段 - Map several fields to List with a mapstruct 如何创建列表<object>带有字段字符串和 Map <string, set<string> &gt; 从另一个列表<object2><div id="text_translate"><p> Class Object2具有标准的 getter 并具有String字段folder 、 file和version 。 它被命名为SourceInfo</p><p> List&lt;SourceInfo&gt; source包含上面提到的三个字段。</p><p> 我的目标是从List&lt;SourceInfo&gt;创建一个List&lt;Info&gt; &gt; 。</p><p> 新List的 class 为Info ,如下图所示。</p><pre> public class Info { private final String folder; private final Map&lt;String, Set&lt;String&gt;&gt; file; public static Builder builder() { return new Builder(); } public static Builder builder(Info info) { return new Builder(info); } private Info(Builder builder) { this.folder = builder.folder; this.file = builder.file; } public String getFolder() { return folder; } public Map&lt;String, Set&lt;String&gt;&gt; getFile() { return file; } // autogenerated toString, hashCode, and equals public static class Builder { private String folder; private Map&lt;String, Set&lt;String&gt;&gt; file; private Builder() {} private Builder(Info info) { this.folder = info.folder; this.file = info.file; } public Builder with(Consumer&lt;Builder&gt; consumer) { consumer.accept(this); return this; } public Builder withFolder(String folder) { this.folder = folder; return this; } public Builder withFile(Map&lt;String, Set&lt;String&gt;&gt; file) { this.file = file; return this; } public Info build() { return new Info(this); } }</pre><p> 到目前为止,我尝试的是在构建器模式中创建一个集合。</p><pre> List&lt;SourceInfo&gt; source; // error: gc overhead limit exceeded List&lt;Info&gt; infoList = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet())))).build()).collect(Collectors.toList()); Map&lt;String, Set&lt;String&gt;&gt; map = source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet()))); List&lt;Info&gt; info = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))).build()).collect(Collectors.toList());</pre><p> 所需的 output。 以下语法可能已关闭。</p><pre> // [String, Map&lt;String, Set&lt;String&gt;&gt;] Info [folder, [key=file [value=version]]]...</pre><p> 我是 Java 的新手,不胜感激。</p><p> 我想了解如何使用 java8 和 for 循环来做到这一点。</p><p> 谢谢你。</p></div></object2></string,></object> - How to create a List<Object> with fields String and Map<String, Set<String>> from another List<Object2> 将列表中的元素映射到另一个列表中的位置 - Map elements in a list to positions in another list Java 8将列表映射到另一个列表并计数 - Java 8 Map a List to another list and count 如何将 map 通用列表转换为另一个通用列表? - How to map generic list into another generic list? Java Map 具有列表值,在另一个列表上过滤 - Java Map with list value, filter on another list map 将对象列表转换为另一个对象列表的方法 - Ways to map the list of objects to another list of objects 如何将列表字段转换为java中的映射 - how to convert list fields to map in java 从列表中检索值 <Map> 并将其插入另一个列表 <Map> - Retrieve value from List<Map> and insert it into another List<Map> Java 8 create将复杂类型的列表映射到其字段之一的列表 - Java 8 create map a list of a complex type to list of one of its fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM