简体   繁体   English

Java 8 - 如何在另一个列表中的 object 的列表中设置 object?

[英]Java 8 - How to set an object in a list from an object in another list?

I want to understand how can I copy/assign an object from a list to another list?我想了解如何将 object 从列表复制/分配到另一个列表?

For example: ClassA has an object of class Sample and ClassB also has an object of class Sample (other data members are different in both the classes as you can see in the example below). For example: ClassA has an object of class Sample and ClassB also has an object of class Sample (other data members are different in both the classes as you can see in the example below).

I have lists of ClassA and ClassB .我有ClassAClassB的列表。

Now if I want to set the object of class Sample in each element of List< ClassB > with the value of the Sample object from List< ClassA > , how can I do this using Functional Style in Java? Now if I want to set the object of class Sample in each element of List< ClassB > with the value of the Sample object from List< ClassA > , how can I do this using Functional Style in Java?

I have shown how it can be done by Imperative Style but I really want to use streams here.我已经展示了如何通过命令式风格来完成它,但我真的想在这里使用


Assumption: sizes of List< ClassA > and List< ClassB > are equal假设: List<ClassA>List<ClassB>的大小相等


These are my classes:这些是我的课:

Sample:样本:

class Sample {
    int a;
    String str;

    public Sample() {}

    public Sample(int a, String str) {
        this.a = a;
        this.str = str;
    }

    @Override
    public String toString() {
        return "Sample [a=" + a + ", str=" + str + "]";
    }
}

ClassA: A类:

class ClassA {
    String someString;
    Sample sample;

    public ClassA(String someString, Sample sample) {
        this.someString = someString;
        this.sample = sample;
    }

    @Override
    public String toString() {
        return "ClassA [someString=" + someString + ", sample=" + sample + "]";
    }
}

ClassB: B类:

class ClassB {
    int someNumber;
    int someOtherNumber;
    String someString;
    Sample sample;

    public ClassB(int someNumber, int someOtherNumber, String someString, Sample sample) {
        super();
        this.someNumber = someNumber;
        this.someOtherNumber = someOtherNumber;
        this.someString = someString;
        this.sample = sample;
    }

    @Override
    public String toString() {
        return "ClassB [someNumber=" + someNumber + ", someOtherNumber=" + someOtherNumber + ", someString="
                + someString + ", sample=" + sample + "]";
    }
}

And finally the main class:最后是主要的 class:

public class ClassC {
    public static void main(String args[]) {

        ClassA a1 = new ClassA("abc", new Sample(1, "abc"));
        ClassA a2 = new ClassA("def", new Sample(2, "abcde"));
        ClassA a3 = new ClassA("pqr", new Sample(3, "abcdf"));
        List<ClassA> listOfA = new ArrayList<>();
        listOfA.add(a1);
        listOfA.add(a2);
        listOfA.add(a3);
        System.out.println(listOfA);

        ClassB b1 = new ClassB(100, 200, "zmr", null);
        ClassB b2 = new ClassB(101, 201, "tpu", null);
        ClassB b3 = new ClassB(103, 203, "zzz", null);
        List<ClassB> listOfB = new ArrayList<>();
        listOfB.add(b1);
        listOfB.add(b2);
        listOfB.add(b3);
        System.out.println("before : " + listOfB);

        // how to do this using stream ?
        for(int i = 0; i < listOfB.size(); i++) {
            listOfB.get(i).sample = listOfA.get(i).sample;
        }

        System.out.println("after : " + listOfB);
    }
}

Output: Output:

[ClassA [someString=abc, sample=Sample [a=1, str=abc]], ClassA [someString=def, sample=Sample [a=2, str=abcde]], ClassA [someString=pqr, sample=Sample [a=3, str=abcdf]]]

before : [ClassB [someNumber=100, someOtherNumber=200, someString=zmr, sample=null], ClassB [someNumber=101, someOtherNumber=201, someString=tpu, sample=null], ClassB [someNumber=103, someOtherNumber=203, someString=zzz, sample=null]]

after : [ClassB [someNumber=100, someOtherNumber=200, someString=zmr, sample=Sample [a=1, str=abc]], ClassB [someNumber=101, someOtherNumber=201, someString=tpu, sample=Sample [a=2, str=abcde]], ClassB [someNumber=103, someOtherNumber=203, someString=zzz, sample=Sample [a=3, str=abcdf]]]

You can do it using IntStream in functional way but honestly both are same您可以使用IntStream以功能方式执行此操作,但老实说两者都是相同的

IntStream.range(0, listOfB.size())
         .forEach(i -> listOfB.get(i).sample = listOfA.get(i).sample);

Here's another way, using an AtomicInteger (so we can make it final) to loop through listB这是另一种方式,使用 AtomicInteger (所以我们可以使它成为最终的)循环 listB

   final AtomicInteger i= new AtomicInteger();
   listOfA.forEach( a-> listOfB.get(i.getAndIncrement()).sample = a.sample );

or the Iterator way或迭代器方式

   Iterator<ClassB> iter = listOfB.iterator(); 
   listOfA.forEach( a-> iter.next().sample = a.sample );

如何创建列表<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.

相关问题 如何在java 8中迭代对象数组列表并设置为另一个对象列表? - How to iterate List of object array and set to another object list in java 8? 使用java流从另一个对象列表更新对象列表 - Updating a List of Object from another list of object using java stream 如何将列表 object 的值设置到另一个列表 object 中? - How to set a value of List object into another List object? Java 流设置 object 列表的每个属性与另一个列表 - Java streams set each attribute of object List with another List 如何创建列表<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> 如何从具有公共字段的另一个对象列表中为一个对象列表的字段设置值? - How to set value for a field of one list of object from another list of object having a common field? Java从另一个类获取对象列表 - Java Get the List of an Object from another Class 用Java 8过滤对象列表与另一个列表 - Filtering a list of list of object with another list in Java 8 从 java 8 中的另一个列表 lambda 表达式更新 object 的列表 - Update list of object from another list lambda expressions in java 8 如何从List对象中的新对象中设置属性值,并获得使用Java 8设置属性的相同对象的新List? - How to set property values in new object from object from List and get a new List of the same objects in which properties are set using java 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM