现有一个list1<ClassA>
classA{
String s1;
String s2;
}
例如:list1中内容:【动物,猫】,【动物,狗】,【植物,花】,【植物,树】
要求将其转换为list2<ClassB>
classB{
String s1;
List list;
}
上例应得结果为:【动物,list(猫,狗)】【植物,list(花,树)】
classA{
String s1;
String s2;
}
例如:list1中内容:【动物,猫】,【动物,狗】,【植物,花】,【植物,树】
要求将其转换为list2<ClassB>
classB{
String s1;
List list;
}
上例应得结果为:【动物,list(猫,狗)】【植物,list(花,树)】
遍历list1时进行判断,if s1=“动物”list2的第一个元素中的list进行add.
import java.util.List;public class Test { public static void main(String[] args) {
List<classA> list1 = new ArrayList<classA>();
list1.add(new classA("动物", "猫"));
list1.add(new classA("动物", "狗"));
list1.add(new classA("植物", "花"));
list1.add(new classA("植物", "树"));
List<classB> list2 = new ArrayList<classB>(); for (int i = 0; i < list1.size(); i++) {
if (list2.size() <= 0) {
classB b = new classB();
b.setS1(list1.get(i).getS1());
List<String> l = new ArrayList<String>();
l.add(list1.get(i).getS2());
b.setList(l);
list2.add(b);
} else {
int j;
boolean has = false;
for (j = 0; j < list2.size(); j++) {
if (list2.get(j).getS1().equals(list1.get(i).getS1())) {
has = true;
break;
}
}
if (has) {
classB b = list2.get(j);
b.getList().add(list1.get(i).getS2());
} else {
classB b = new classB();
b.setS1(list1.get(i).getS1());
List<String> l = new ArrayList<String>();
l.add(list1.get(i).getS2());
b.setList(l);
list2.add(b);
}
}
}
System.out.println(list2); }}class classA {
private String s1; private String s2; public classA(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
} public String getS1() {
return s1;
} public void setS1(String s1) {
this.s1 = s1;
} public String getS2() {
return s2;
} public void setS2(String s2) {
this.s2 = s2;
}
}class classB {
private String s1; private List<String> list; public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public String getS1() {
return s1;
} public void setS1(String s1) {
this.s1 = s1;
} public String toString() {
return s1 + list.toString();
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;public class Test1 { public static void main(String[] args) {
List<RtuAlertCode2> list1 = new ArrayList<RtuAlertCode2>();
list1.add(new RtuAlertCode2("动物", "猫"));
list1.add(new RtuAlertCode2("动物", "狗"));
list1.add(new RtuAlertCode2("植物", "花"));
list1.add(new RtuAlertCode2("植物", "树"));
list1.add(new RtuAlertCode2("其他", "凳子"));
list1.add(new RtuAlertCode2("其他", "椅子"));
Map<String , RtuAlertCode> map = new HashMap<String, RtuAlertCode>();
for(int i=0;i<list1.size();i++){
RtuAlertCode2 rc2 = (RtuAlertCode2)list1.get(i);
String key = rc2.getS1();
RtuAlertCode rvalue= map.get(key);
List<String> l = new ArrayList<String>();
if(rvalue==null){
rvalue = new RtuAlertCode();
l.add(rc2.getS2());
rvalue.setS1(key);
rvalue.setList(l);
}else{
l = rvalue.getList();
l.add(rc2.getS2());
rvalue.setS1(key);
rvalue.setList(l);
}
map.put(key, rvalue);
}
Iterator it = map.entrySet().iterator();
List<RtuAlertCode> list2 = new ArrayList<RtuAlertCode>();
while (it.hasNext()){
Map.Entry entry =(Map.Entry)it.next();
String key = entry.getKey().toString();
RtuAlertCode value = (RtuAlertCode)entry.getValue();
list2.add(value);
}
System.out.println(list2); }}class RtuAlertCode2 {
private String s1; private String s2; public RtuAlertCode2(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
} public String getS1() {
return s1;
} public void setS1(String s1) {
this.s1 = s1;
} public String getS2() {
return s2;
} public void setS2(String s2) {
this.s2 = s2;
}
}class RtuAlertCode {
private String s1; private List<String> list; public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public String getS1() {
return s1;
} public void setS1(String s1) {
this.s1 = s1;
} public String toString() {
return s1 + list.toString();
}
}
class classA {
String s1;
String s2; public classA(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
}
} class classB {
String s1;
List<String> list = new ArrayList<String>();
} public static void main(String[] args) throws Exception {
new Test().go();
} private void go() {
List<classA> list1 = new ArrayList<classA>();
list1.add(new classA("动物", "猫"));
list1.add(new classA("动物", "狗"));
list1.add(new classA("植物", "花"));
list1.add(new classA("植物", "树"));
Map<String, classB> map = new HashMap<String, classB>();
for(classA a : list1) {
classB b = map.get(a.s1);
if(b == null) {
b = new classB();
map.put(a.s1, b);
}
b.list.add(a.s2);
}
List<classB> list2 = new ArrayList<classB>(map.values());
}
}