0

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

我有一张清单,里面有一张地图

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);

我试过这个,但它不能正常工作。

4

2 回答 2

1

根据我的说法,您应该为您的 id、no、name 值创建 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);
    }
于 2012-07-17T04:34:07.377 回答
0

只有在适合您的情况下,我才会建议其他方式。该解决方案基于创建专用类hashCodeequals重写方法,允许标准数据结构在您身上工作。您需要 hashCode 来返回您的实体的 id 值和 equals 方法来比较 id 是否相等,并比较不比较任何值,而不是只允许具有更大值的实体替换以前的实体。请参阅代码:

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]]

希望这将有助于并适合您的任务。

于 2012-07-17T06:23:24.670 回答