简体   繁体   中英

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?

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 .

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


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:

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:

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:

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:

[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.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

   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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM