Java中多个List合并

时间:2021-11-23 20:34:28


天猫内部优惠券

开发中需要用到list合并,之前没用过,记录一下,


import java.util.ArrayList;
import java.util.List;
import Bean.Entity;
public class SumList {

public static void SumList() {
List<Entity> rs = new ArrayList<Entity>();
List<Entity> rs2 = new ArrayList<Entity>();
Entity en;

for (int i = 0; i < 3; i++) {
en = new Entity();
en.setId("rs---" + i);

rs.add(en);
}
for (int i = 0; i < 4; i++) {
en = new Entity();
en.setId("rs2---" + i);

rs2.add(en);
}

rs.addAll(rs2);
for (int i = 0; i < rs.size(); i++) {

System.out.println(rs.get(i).getId());

}
}

public static void main(String[] args) {
SumList();
}
}



运行结果:

rs---0
rs---1
rs---2
rs2---0
rs2---1
rs2---2
rs2---3