Java Map按照Key和Value排序【轉】

來源:互聯網
上載者:User

標籤:logs   class   for   ortmap   color   while   排序   dha   ==   

 

package kingtool.sort;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.TreeMap;import java.util.Map.Entry;/** *  * @author King * */public class MapSortTool {    public static void main(String[] args) {        Map<String, String> map = new TreeMap<String, String>();        map.put("4", "2");        map.put("1", "1");        map.put("9", "3");        map.put("8", "6");//      Map<String, String> resultMap = sortMapByKey(map); //按Key進行排序        Map<String, String> resultMap = sortMapByValue(map); // 按Value進行排序        for (Map.Entry<String, String> entry : resultMap.entrySet()) {            System.out.println(entry.getKey() + " " + entry.getValue());        }    }    /**     * 使用 Map按key進行排序     *      * @param map     * @return     */    public static Map<String, String> sortMapByKey(Map<String, String> map) {        if (map == null || map.isEmpty()) {            return null;        }        Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyStringComparator());        sortMap.putAll(map);        return sortMap;    }    /**     * 使用 Map按value進行排序     *      * @param map     * @return     */    public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {        if (oriMap == null || oriMap.isEmpty()) {            return null;        }        Map<String, String> sortedMap = new LinkedHashMap<String, String>();        List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet());        Collections.sort(entryList, new MapValueStringComparator());        Iterator<Map.Entry<String, String>> iter = entryList.iterator();        Map.Entry<String, String> tmpEntry = null;        while (iter.hasNext()) {            tmpEntry = iter.next();            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());        }        return sortedMap;    }    /**     * string key升序     * @author King     *     */    static class MapKeyStringComparator implements Comparator<String>{        @Override        public int compare(String str1, String str2) {            return str1.compareTo(str2);        }    }        /**     * string value升序     * @author King     *     */    static class MapValueStringComparator implements Comparator<Map.Entry<String, String>> {        @Override        public int compare(Entry<String, String> entry1, Entry<String, String> entry2) {            return entry1.getValue().compareTo(entry2.getValue());        }    }            /**     * tip: 範例中沒用到<br/>     * double value降序     * @author King     *     */    static class MapValueNumberComparator implements Comparator<Map.Entry<String, Double>> {        @Override        public int compare(Entry<String, Double> entry1, Entry<String, Double> entry2) {            return entry1.getValue().compareTo(entry2.getValue());        }    }}

 

Java Map按照Key和Value排序【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.