java list 删除重复的,借助map

来源:互联网 发布:js删除集合对象元素 编辑:程序博客网 时间:2024/06/06 02:26
 // 删除ArrayList中重复元素,    public   static     List<FutureNeed>  removeDuplicate( List<FutureNeed> list)  {        List<FutureNeed> tempList=new ArrayList<>();        Map<String, String> map = new HashMap<String, String>();        for (int i = 0; i < list.size(); i++) {            String key = list.get(i).getManufacturerName()+list.get(i).getMpn()+list.get(i).getRegion()+list.get(i).getPackaging();            String old = map.get(key);            if (old != null) {                map.put(key, old + "," + (i + 1));            } else {                map.put(key, "" + (i + 1));            }        }        Iterator<String> it = map.keySet().iterator();        while (it.hasNext()) {            String key = it.next();            String value = map.get(key);            if (value.indexOf(",") != -1) {//                System.out.println(key + " 重复,行: " + value);                String temp[]=value.split(",");                for(int i=0;i<temp.length;i++){                    tempList.add(list.get(Integer.valueOf(temp[i])-1));                }            }        }        list.removeAll(tempList);        return list;    }
原创粉丝点击