如何迭代地图列表并找到字段的最大值并删除其他字段

时间:2012-07-17 03:27:48

标签: java list map

如何迭代地图列表并找到字段的最大值

我有一个列表,其中有一张地图

listGrid={{id=5,name="person",no=9},{id=6,name="person",no=19},{id=6,name="jam",no=10}}

我想要结果div,因为它应该删除id的副本并且其中最多没有字段

    List<Map> listGrid=new ArrayList<Map>();
    Map<String,Object> resultMap=new HashMap<String,Object>();
    resultMap.put("id", "5");
    resultMap.put("name", "one");
    resultMap.put("no", 1);
    listGrid.add(resultMap);
    resultMap.put("id", "5");
    resultMap.put("name", "one");
    resultMap.put("no", 11);
    listGrid.add(resultMap);
    resultMap.put("id", "1");
    resultMap.put("name", "one");
    resultMap.put("no", 5);

    List list=new ArrayList();
     Map  mapList=new HashMap();
       ListIterator litr = listGrid.listIterator(); 
        while(litr.hasNext()) {

            Map element = (Map)litr.next(); 
            String id= (String) element.get("id");
            Integer damagesNo = (Integer) element.get("no");
            if(mapList.containsKey(id)){
            Integer mapCode = (Integer) mapList.get(id);
            int damagesNoInt = damagesNo;
            int mapCodeInt = mapCode;
            if(damagesNoInt <= mapCodeInt){
                int i=litr.nextIndex()-1;
                list.add(i);
            }
            else{
                mapList.put(id, damagesNo);

            }}
            else{
                mapList.put(id, damagesNo);
            }
        }
        System.out.println(listGrid);

我试过这个,但它无法正常工作。

2 个答案:

答案 0 :(得分:1)

根据我的说法,您应该为您的id,no,名称值制作DTO类对象,并对它们进行处理,这将是一种正确的方法

试试此代码

public static void main(String[] args) throws IOException {

        List<Map> listGrid = new ArrayList<Map>();

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("id", "5");
        resultMap.put("name", "person");
        resultMap.put("no", 9);
        listGrid.add(resultMap);
        resultMap = new HashMap<String, Object>();
        resultMap.put("id", "6");
        resultMap.put("name", "person");
        resultMap.put("no", 19);
        listGrid.add(resultMap);
        resultMap = new HashMap<String, Object>();
        resultMap.put("id", "6");
        resultMap.put("name", "jam");
        resultMap.put("no", 21);
        listGrid.add(resultMap);
        resultMap = new HashMap<String, Object>();
        resultMap.put("id", "6");
        resultMap.put("name", "jam");
        resultMap.put("no", 21);
        listGrid.add(resultMap);
        List<Map> destListGrid = new ArrayList<Map>();
        HashMap<String, Object> destMap = new HashMap<String, Object>();

        ListIterator litr = listGrid.listIterator();
        while (litr.hasNext()) {

            HashMap<String, Object> element = (HashMap<String, Object>) litr
                    .next();
            String id = (String) element.get("id");
            Integer damagesNo = (Integer) element.get("no");
            if (destMap.containsKey(id)) {
                Integer mapCode = (Integer) destMap.get(id);
                if (mapCode <= damagesNo) {
                    destMap.remove(id);
                    destMap.put(id, damagesNo);
                }
            } else {
                destMap.put(id, damagesNo);
            }
        }

        for (Map map : listGrid) {

            if (destMap.containsKey(map.get("id"))
                    && destMap.get(map.get("id")) == map.get("no")) {
                            destMap.remove(map.get("id"));  
                destListGrid.add(map);
            }
        }

        System.out.println(destListGrid);
    }

答案 1 :(得分:0)

如果它适合你,我会建议另一种方式。此解决方案基于创建具有hashCodeequals方法的专用类,它允许标准数据结构对您起作用。您需要使用hashCode返回实体的id值,并使用equals方法比较id的相等性,并且不比较grater的值,而只允许具有更大值的实体替换前者。请参阅代码plz:

public class MapList {

static class Entity {

    int id;
    String name;
    int no;

    public int hashCode() {
        return id;
    }

    public boolean equals(Object o) {
        if (o instanceof Entity) {
            Entity entity = (Entity) o;
            return id == entity.id && entity.no >= no;
        }
        return false;
    }

    public Entity(int id, String name, int no) {
        this.id = id;
        this.name = name;
        this.no = no;
    }

    public String toString() {
        return String.format("[id=%d,name='%s',no=%d]", id, name, no);
    }

}

public static void main(String[] args) {
    List<Entity> initialList = new ArrayList<Entity>();
    initialList.add(new Entity(5, "person", 9));
    initialList.add(new Entity(6, "person", 19));
    initialList.add(new Entity(6, "jam", 10));
    System.out.println(initialList);
    Set<Entity> magicCleanSet = new HashSet<Entity>();
    magicCleanSet.addAll(initialList);
    System.out.println(magicCleanSet);
}

}

输出结果为:

[[id=5,name='person',no=9], [id=6,name='person',no=19], [id=6,name='jam',no=10]]
[[id=5,name='person',no=9], [id=6,name='person',no=19]]

希望这对您的任务有所帮助。