在JAVA中,想要把一个list中的内容添加至另一个list中,有两种方法:
采用循环的方法。通过循环第一个ArrayList,然后通过每循环一次的方式,将值赋值给另一个ArrayList。具体代码实现如下:
2采用ArrayList自带的addAll()方法。addAll()方法是通过传入整一个List,将此List中的所有元素加入到新的List中,也就是当前List会增加的元素个数等于传入的List的大小。该方法较为简单,具体代码实现如下:
List<ProductStorehouseInput> list1 = daolistproInputsinfo(idInteger);Iterator it = list1iterator();
Map map = new HashMap();
while(ithasNext()){
ProductStorehouseInput psi = (ProductStorehouseInput)itnext();
String key = psiget仓库号();
if(mapget(key) == null){
mapput(key,psi);
}else {
ProductStorehouseInput psi1 = (ProductStorehouseInput)mapget(key);
psi1setNumber(psi1getNumber() + psigetNumber());
mapput(key,psi1);
}
list1remove(psi);
}
for(Object obj:mapkeySet()){
list1add(mapget(obj));
}
batchid,genmankbn,companycd是啥?
对象,对象里面的属性值相同的合并?
合并到一个list里面,最后在有list封装?
给你了例子:
import javautilArrayList;import javautilCollections;
import javautilComparator;
import javautilList;
public class $ {
public static void main(String[] args) {
List<Bean> data = new ArrayList<Bean>();
// 初始化
init(data);
Systemoutprintln(data);
// 排序
sort(data);
Systemoutprintln(data);
// 分组
List<List<Bean>> result = group(data);
Systemoutprintln(result);
// 数据合并
List<Bean> beanList = hebing(result);
Systemoutprintln(beanList);
}
private static List<Bean> hebing(List<List<Bean>> result) {
List<Bean> data = new ArrayList<Bean>();
for (List<Bean> tmp : result) {
String text = "";
for (Bean bean : tmp) {
text += beangetText() + ",";
}
Bean bean0 = tmpget(0);
bean0setText(textsubstring(0, textlength() - 1));
dataadd(bean0);
}
return data;
}
private static List<List<Bean>> group(List<Bean> data) {
List<List<Bean>> result = new ArrayList<List<Bean>>();
String oldVal = "";
List<Bean> list = new ArrayList<Bean>();
for (Bean bean : data) {
String newVal = beangetBatchid() + "_" + beangetGenmankbn() + "_" + beangetCompanycd();
if (!oldValequalsIgnoreCase(newVal)) {
if (!isNotNullOrBlank(list)) {
resultadd(list);
}
list = null;
oldVal = newVal;
}
if (list == null) {
list = new ArrayList<Bean>();
}
listadd(bean);
}
if (!isNotNullOrBlank(list)) {
resultadd(list);
}
return result;
}
private static void sort(List<Bean> data) {
Collectionssort(data, new Comparator<Bean>() {
public int compare(Bean o1, Bean o2) {
int compare = o1getBatchid() - o2getBatchid();
if (compare == 0) {
compare = o1getGenmankbn() - o2getGenmankbn();
if (compare == 0) {
compare = o1getCompanycd() - o2getCompanycd();
if (compare == 0) {
return o1getText()compareTo(o2getText());
}
}
}
return compare;
}
});
}
private static boolean isNotNullOrBlank(List<Bean> list) {
return list == null || listisEmpty();
}
private static void init(List<Bean> data) {
Bean bean1 = new Bean(1, 1, 1, "1");
Bean bean2 = new Bean(1, 1, 1, "2");
Bean bean3 = new Bean(1, 1, 1, "3");
Bean bean4 = new Bean(1, 2, 3, "4");
Bean bean5 = new Bean(1, 2, 3, "5");
Bean bean6 = new Bean(1, 2, 4, "6");
dataadd(bean1);
dataadd(bean2);
dataadd(bean3);
dataadd(bean4);
dataadd(bean5);
dataadd(bean6);
}
}
class Bean {
private int batchid;
private int genmankbn;
private int companycd;
private String text;
public Bean(int batchid, int genmankbn, int companycd, String text) {
thisbatchid = batchid;
thisgenmankbn = genmankbn;
thiscompanycd = companycd;
thistext = text;
}
public int getBatchid() {
return batchid;
}
public void setBatchid(int batchid) {
thisbatchid = batchid;
}
public int getGenmankbn() {
return genmankbn;
}
public void setGenmankbn(int genmankbn) {
thisgenmankbn = genmankbn;
}
public int getCompanycd() {
return companycd;
}
public void setCompanycd(int companycd) {
thiscompanycd = companycd;
}
public String getText() {
return text;
}
public void setText(String text) {
thistext = text;
}
public String toString() {
return "{" + batchid + "," + genmankbn + "," + companycd + ",'" + text + "'}";
}
}
结果:
[{1,1,1,'1'}, {1,1,1,'2'}, {1,1,1,'3'}, {1,2,3,'4'}, {1,2,3,'5'}, {1,2,4,'6'}]
[{1,1,1,'1'}, {1,1,1,'2'}, {1,1,1,'3'}, {1,2,3,'4'}, {1,2,3,'5'}, {1,2,4,'6'}]
[[{1,1,1,'1'}, {1,1,1,'2'}, {1,1,1,'3'}], [{1,2,3,'4'}, {1,2,3,'5'}], [{1,2,4,'6'}]]
[{1,1,1,'1,2,3'}, {1,2,3,'4,5'}, {1,2,4,'6'}]
for(int i = listsize(); i >=0; i--) {
User user = (User)listget(i);
User temp;
for(int j = 0; j < listsize(); j ++) {
if(userget("time")equals(tempget("time"))) {
listremove(j);
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)