删除复杂List中的重复数据
package utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListUtil {
?
?public static void main(String[] args) {
??List list = new ArrayList();
??Map map1 = new HashMap<String,String>();
??map1.put("id", "001");
??map1.put("name", "张三");
??Map map2 = new HashMap<String,String>();
??map2.put("id", "001");
??map2.put("name", "李四");
??Map map3 = new HashMap<String,String>();
??map3.put("id", "002");
??map3.put("name", "王二");
??Map map4 = new HashMap<String,String>();
??map4.put("id", "003");
??map4.put("name", "赵五");
??Map map5 = new HashMap<String,String>();
??map5.put("id", "004");
??map5.put("name", "陈六");
??list.add(map1);
??list.add(map2);
??list.add(map3);
??list.add(map4);
??list.add(map5);
??
??for (int i = 0; i < list.size(); i++) {
???Map map = (Map) list.get(i);
???System.out.println(map.get("name"));
??}
??
??System.out.println("----------------------------");
??
??int count = getList(list);
??
??for (int i = 0; i < list.size(); i++) {
???Map map = (Map) list.get(i);
???System.out.println(map.get("name"));
??}
?}
?
?public static int getList(List src){
??
??List list1 = new ArrayList();
??List list2 = new ArrayList();
??int oldSize = src.size();
??for (int i = 0; i < src.size(); i++) {
???Map map = (Map) src.get(i);
???String id = (String) map.get("id");
???if(!list1.contains(id)){
????list1.add(id);
???}else{
????src.remove(i);
????list2.add(map);
???}
??}
??
??for (int i = 0; i < src.size(); i++) {
???Map map = (Map) src.get(i);
???String id = (String) map.get("id");
???String name = (String) map.get("name");
???for (int j = 0; j < list2.size(); j++) {
????String id1 = (String) ((Map)list2.get(j)).get("id");
????String name1 = (String) ((Map)list2.get(j)).get("name");
????if(id.equals(id1)){
?????map.put("name", name+","+name1);
????}
???}
??}
??
??return oldSize - src.size();
??
?}
}