去除List中的map中的重复值

package ryj;


import java.util.*;


public class _1 {


    /**
     * @param args
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map msp = new HashMap();
        List> list = new ArrayList>();
        List> listMap = new ArrayList>();
        Map map1 = new HashMap();
        map1.put("COL0", "1");
        map1.put("name", "11");
        Map map2 = new HashMap();
        map2.put("COL0", "2");
        map2.put("name", "11");
        Map map3 = new HashMap();
        map3.put("COL0", "3");
        map3.put("name", "11");
        Map map4 = new HashMap();
        map4.put("COL0", "4");
        map4.put("name", "44");
        Map map5 = new HashMap();
        map5.put("COL0", "5");
        map5.put("name", "55");
        Map map6 = new HashMap();
        
        map6.put("COL0", "5");
        map6.put("name", "55");
        Map map7 = new HashMap();
        map7.put("COL0", "6f");
        map7.put("name", "66");
        Map map8 = new HashMap();
        map8.put("COL0", "6");
        map8.put("name", "77");
        Map map9 = new HashMap();
        map9.put("COL0", "8");
        map9.put("name", "f1");
        
        list.add(map1);
        list.add(map3);
        list.add(map2);
        list.add(map4);
        list.add(map5);
        list.add(map6);
        list.add(map7);
        list.add(map8);
        list.add(map9);
        
        System.out.println("初始数据:" + list.toString());
        
        //把list中的数据转换成msp,去掉同一COL0值多余数据,保留查找到第一个COL0值对应的数据
        for(int i = list.size()-1 ; i>=0; i--){
            Map map = list.get(i);
            String COL0 = (String)map.get("COL0");
            map.remove("COL0");
            msp.put(COL0, map);
        }
         //把msp再转换成list,就会得到根据某一字段去掉重复的数据的List
        Set mspKey = msp.keySet();
        for(String key: mspKey){
            Map newMap = msp.get(key);
            newMap.put("COL0", key);
            listMap.add(newMap);
        }
        list=listMap;
        System.out.println("去掉重复数据后的数据:" + list.toString());
    }
}

你可能感兴趣的:(Java)