java List 倒序排序

來源:互聯網
上載者:User
package com.shiyu.client.frame.utils;import java.util.*;import java.util.Map.Entry;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by Administrator on 2017/1/3. */public class CountSortList {    /**     * list倒序排序     * @param strs     * @return     */    public static List<String> countStr(List<String> strs) {        Map<String, Integer> map = new HashMap<String, Integer>();        Set<String> set = new HashSet<String>(strs);        for (String str : set) {            for (String lstr : strs) {                if (str.equals(lstr)) {                    if (map.containsKey(str)) {                        Integer count = map.get(str);                        count++;                        map.put(str, count);                    } else {                        map.put(str, 1);                    }                }            }        }        strs.clear();        Map<String, Integer> sortMap = sortMapByValue(map);        for (Map.Entry<String, Integer> entry : sortMap.entrySet()) {            strs.add(entry.getKey());        }        return strs;    }    /**     * Map倒序排序     * @param map     * @return     */    private static Map<String, Integer> sortMapByValue(Map<String, Integer> map) {        List<Map.Entry<String, Integer>> mapList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());        Collections.sort(mapList, new Comparator<Map.Entry<String, Integer>>() {            @Override            public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {                return o2.getValue() - o1.getValue();            }        });        Map<String, Integer> result = new LinkedHashMap<String, Integer>();        for (Map.Entry<String, Integer> entry : mapList) {            result.put(entry.getKey(), entry.getValue());        }        return result;    }    public Map<String, String> sortMapByKey(Map<String, String> oriMap) {        if (oriMap == null || oriMap.isEmpty()) {            return null;        }        Map<String, String> sortedMap = new TreeMap<String, String>(  new Comparator<String>() {                    public int compare(String key1, String key2) {                        int intKey1 = 0, intKey2 = 0;                        try {                            intKey1 = getInt(key1);                            intKey2 = getInt(key2);                        } catch (Exception e) {                            intKey1 = 0;                            intKey2 = 0;                        }                        return intKey1 - intKey2;                    }                });        sortedMap.putAll(oriMap);        return sortedMap;    }    private int getInt(String str) {        int i = 0;        try {            Pattern p = Pattern.compile("^\\d+");            Matcher m = p.matcher(str);            if (m.find()) {                i = Integer.valueOf(m.group());            }        } catch (NumberFormatException e) {            e.printStackTrace();        }        return i;    }    public static void main(String[] args) {        List<String> strs = Arrays.asList("a", "b", "c", "d", "e", "a", "a",                "a", "a", "b", "b", "b");        Map<String, Integer> map = new HashMap<String, Integer>();        Set<String> set = new HashSet<String>(strs);        for (String str : set) {            for (String lstr : strs) {                if (str.equals(lstr)) {                    if (map.containsKey(str)) {                        Integer count = map.get(str);                        count++;                        map.put(str, count);                    } else {                        map.put(str, 1);                    }                }            }        }        Map<String, Integer> sortMap = sortMapByValue(map);        for (Map.Entry<String, Integer> entry : sortMap.entrySet()) {            System.out.println("Key : " + entry.getKey() + " Value : "                    + entry.getValue());        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.