![](/img/trans.png)
[英]How to iterate List of object array and set to another object list in java 8?
[英]Java 8 - How to set an object in a list from an object in 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).
我有ClassA和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?
我已经展示了如何通过命令式风格来完成它,但我真的想在这里使用流。
假设: List<ClassA>和List<ClassB>的大小相等
这些是我的课:
样本:
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 + "]";
}
}
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 + "]";
}
}
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 + "]";
}
}
最后是主要的 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]]]
您可以使用IntStream以功能方式执行此操作,但老实说两者都是相同的
IntStream.range(0, listOfB.size())
.forEach(i -> listOfB.get(i).sample = listOfA.get(i).sample);
这是另一种方式,使用 AtomicInteger (所以我们可以使它成为最终的)循环 listB
final AtomicInteger i= new AtomicInteger();
listOfA.forEach( a-> listOfB.get(i.getAndIncrement()).sample = a.sample );
或迭代器方式
Iterator<ClassB> iter = listOfB.iterator();
listOfA.forEach( a-> iter.next().sample = a.sample );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.