如何迭代地图列表,并找到一个字段的最大值并删除其他 [英] How to iterate list of map and find max of a field and remove other

查看:123
本文介绍了如何迭代地图列表,并找到一个字段的最大值并删除其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to iterate a list of map and find max of a field

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

I have a list with in that there is a map

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

我想要结果div,因为它应该删除ids和具有最大无字段

I want the result div as it should remove the duplicate of ids and having which is have maximum of no field

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

我尝试了这个但是不能正常工作。

I tried with this but it is not working properly.

推荐答案

根据我,你应该为你的id,no,name值做DTO类对象,并对它们进行处理,这将是一个正确的方法

According to me you should make DTO class object for your id,no,name values and do processing on them that would be an proper approach

尝试此代码

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

这篇关于如何迭代地图列表,并找到一个字段的最大值并删除其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆