美文网首页
List> 对Map中k

List> 对Map中k

作者: LX_代码制造坊 | 来源:发表于2020-02-17 13:09 被阅读0次
    public class Sort {
        public static void main(String[] args) {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("name", "p");
            map1.put("cj", "5");
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("name", "h");
            map2.put("cj", "12");
            Map<String, Object> map3 = new HashMap<String, Object>();
            map3.put("name", "f");
            map3.put("cj", "31");
            Map<String, Object> map4 = new HashMap<String, Object>();
            map4.put("name", "f");
            map4.put("cj", "30");
            list.add(map1);
            list.add(map3);
            list.add(map2);
            list.add(map4);
            //排序前
            for (Map<String, Object> map : list) {
                System.out.println(map.get("cj"));
            }
            Collections.sort(list, new Comparator<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Integer name1 = Integer.valueOf(o1.get("cj").toString()) ;//name1是从你list里面拿出来的一个
                    Integer name2 = Integer.valueOf(o2.get("cj").toString()) ; //name1是从你list里面拿出来的第二个name
                    return name1.compareTo(name2);
                }
            });
            //排序后
            System.out.println("-------------------");
            int i = 1;
            for (Map<String, Object> map : list) {
                map.put("paiming",i);
                i++;
                System.out.println(map.toString());
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:List> 对Map中k

          本文链接:https://www.haomeiwen.com/subject/jvsbfhtx.html