栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java按值对HashMap排序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java按值对HashMap排序

尝试下面的代码对我来说很好。你可以选择升序和降序

import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.linkedHashMap;import java.util.linkedList;import java.util.List;import java.util.Map;import java.util.Map.Entry;public class SortMapByValue{    public static boolean ASC = true;    public static boolean DESC = false;    public static void main(String[] args)    {        // Creating dummy unsorted map        Map<String, Integer> unsortMap = new HashMap<String, Integer>();        unsortMap.put("B", 55);        unsortMap.put("A", 80);        unsortMap.put("D", 20);        unsortMap.put("C", 70);        System.out.println("Before sorting......");        printMap(unsortMap);        System.out.println("After sorting ascending order......");        Map<String, Integer> sortedMapAsc = sortByComparator(unsortMap, ASC);        printMap(sortedMapAsc);        System.out.println("After sorting descindeng order......");        Map<String, Integer> sortedMapDesc = sortByComparator(unsortMap, DESC);        printMap(sortedMapDesc);    }    private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order)    {        List<Entry<String, Integer>> list = new linkedList<Entry<String, Integer>>(unsortMap.entrySet());        // Sorting the list based on values        Collections.sort(list, new Comparator<Entry<String, Integer>>()        { public int compare(Entry<String, Integer> o1,         Entry<String, Integer> o2) {     if (order)     {         return o1.getValue().compareTo(o2.getValue());     }     else     {         return o2.getValue().compareTo(o1.getValue());     } }        });        // Maintaining insertion order with the help of linkedList        Map<String, Integer> sortedMap = new linkedHashMap<String, Integer>();        for (Entry<String, Integer> entry : list)        { sortedMap.put(entry.getKey(), entry.getValue());        }        return sortedMap;    }    public static void printMap(Map<String, Integer> map)    {        for (Entry<String, Integer> entry : map.entrySet())        { System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue());        }    }}

编辑:版本2

使用了新的Java功能,例如流for-each等

如果值相同,则地图将按键排序

 import java.util.*; import java.util.Map.Entry; import java.util.stream.Collectors; public class SortMapByValue {    private static boolean ASC = true;    private static boolean DESC = false;    public static void main(String[] args)    {        // Creating dummy unsorted map        Map<String, Integer> unsortMap = new HashMap<>();        unsortMap.put("B", 55);        unsortMap.put("A", 20);        unsortMap.put("D", 20);        unsortMap.put("C", 70);        System.out.println("Before sorting......");        printMap(unsortMap);        System.out.println("After sorting ascending order......");        Map<String, Integer> sortedMapAsc = sortByValue(unsortMap, ASC);        printMap(sortedMapAsc);        System.out.println("After sorting descending order......");        Map<String, Integer> sortedMapDesc = sortByValue(unsortMap, DESC);        printMap(sortedMapDesc);    }    private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap, final boolean order)    {        List<Entry<String, Integer>> list = new linkedList<>(unsortMap.entrySet());        // Sorting the list based on values        list.sort((o1, o2) -> order ? o1.getValue().compareTo(o2.getValue()) == 0     ? o1.getKey().compareTo(o2.getKey())     : o1.getValue().compareTo(o2.getValue()) : o2.getValue().compareTo(o1.getValue()) == 0     ? o2.getKey().compareTo(o1.getKey())     : o2.getValue().compareTo(o1.getValue()));        return list.stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> b, linkedHashMap::new));    }    private static void printMap(Map<String, Integer> map)    {        map.forEach((key, value) -> System.out.println("Key : " + key + " Value : " + value));    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392651.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号