JAVA遍历List<Map<String,Object>> 如何根据List中Map的Value的值进行遍历

    xiaoxiao2021-03-25  109

    这是一个List<Map<String,Object>> List<Map<String,Object>> demoMapList=new ArrayList<Map<String,Object>>(); 我们现在要根据 list元素中Map的其中的一个key对应的  Value进行排序,假如我们根据demoStatus 而且这个demoStatus其实一个Integer类型的。 我们可以用java原生好的集合工具类 进行排序,比如这样: Collections.sort(demoMapList,new Comparator<Map<String,Object>>(){ @Override public int compare(Map<String, Object> o1, Map<String, Object> o2) { return Integer.parseInt(o2.get("demoStatus").toString())-Integer.parseInt(o1.get("demoStatus").toString()); }           }); 我们把一个问题就这样解决了。但是要根据Map<String,Integer> 的value中排序,那么我们如果做呢。它并没有在List中,其实我们也可以用Collections.sort进行排序,思路就是 我们把Map.Entry 放到List之中: Map<String,Integer> map =new HashMap<String,Integer>();         map.put("q", 1);         map.put("r", 4);         map.put("w", 2);         map.put("e", 3);         List<Map.Entry<String,Integer >> list=new ArrayList<Map.Entry<String,Integer >>(map.entrySet());             map.entrySet();                  Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){             @Override             public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {                 return o1.getValue()-o2.getValue();             }                      }  );                  for(Map.Entry<String, Integer> e:list ){             System.out.println(e.getValue()+"----"+e.getKey());         }

    转载请注明原文地址: https://ju.6miu.com/read-15257.html

    最新回复(0)