码迷,mamicode.com
首页 > 编程语言 > 详细

list<map>排序

时间:2017-10-27 16:05:01      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:iter   add   public   tin   compareto   code   sys   lis   rri   

   @Test
    public void test_when_test1() throws Exception {
        String[] strs = {"e", "ee", "ea", "ei", "ie", "i", "y", "e", "e", "ea", "a", "e", "ir", "ar", "or", "ear", "e", "a", "o", "u", "or", "er", "oo", "o", "u", "ew", "ui", "oe", "ou", "oo", "oul", "u", "o", "a", "al", "au", "aw", "or", "oar", "ore", "ar", "oor", "ou", "al", "ar", "a", "o", "u", "ou", "a", "ai", "ay", "ei", "i", "y", "oy", "oi", "o", "oa", "oe", "ow", "ou", "ow", "ear", "ere", "ea", "eer", "are", "air", "ear", "ere", "our", "oor", "ure"};
        log.info("=====>strs.length: " + strs.length);
        List<String> list = Arrays.asList(strs);
        Set set = new HashSet(list);
        Object[] strsvm = set.toArray(new String[0]);
        Arrays.sort(strsvm);
        List<Object> strsvmDm = Arrays.asList(strsvm);
        List<Map> strings = new TreeList();
        for (int i = 0; i < strsvm.length; i++) {
            Object o = strsvm[i];
            int count = 0;
            Map map = new TreeMap();
            for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
                String next =  iterator.next();
                if (next.matches(o.toString())) {
                    count++;
                }
            }
            if (count == 0) count = 1;
            map.put("count", count);
            map.put("value", o);
            strings.add(map);

        }
        if (null != strings&& strings.size()>0) {
            Collections.sort(strings,new Comparator<Map>() {
                @Override
                public int compare(Map o1, Map o2) {
                    int ret = 0;
                    //比较两个对象的顺序,如果前者小于、等于或者大于后者,则分别返回-1/0/1
                    ret = o1.get("count").toString().compareTo(o2.get("count").toString());//逆序的话就用o2.compareTo(o1)即可
                    return ret;
                }
            });
        }
        // 输出
        int sum = 0;
        for (Iterator<Map> iterator = strings.iterator(); iterator.hasNext(); ) {
            Map next =  iterator.next();
//            log.warn("=====>next: " + next);
            Object vmCount = next.get("count");
            Object value = next.get("value");
            System.out.print("=====>value: " + value+"   \t\t"+vmCount);
            Integer vmCountInt = Integer.valueOf(vmCount.toString());
            sum = sum + vmCountInt;
            System.out.println();
        }

    }

 

list<map>排序

标签:iter   add   public   tin   compareto   code   sys   lis   rri   

原文地址:http://www.cnblogs.com/rhxuza1993/p/7742687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!