Map按值进行排序

Map是键值对的集合,又叫作字典或关联数组等,是最常见的数据结构之一。在java如何让一个map按value排序呢? 看似简单,但却不容易!

比如,Map中key是String类型,表示一个单词,而value是int型,表示该单词出现的次数,现在我们想要按照单词出现的次数来排序:

Map map = new TreeMap();
map.put("me", 1000);
map.put("and", 4000);
map.put("you", 3000);
map.put("food", 10000);
map.put("hungry", 5000);
map.put("later", 6000);

按值排序的结果应该是:
key value
me 1000
you 3000
and 4000
hungry 5000
later 6000
food 10000

首先,不能采用SortedMap结构,因为SortedMap是按键排序的Map,而不是按值排序的Map,我们要的是按值排序的Map。

Couldn't you do this with a SortedMap? 
No, because the map are being sorted by its keys.

方法一:

如下Java代码:

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {

        Set set = new TreeSet();
        set.add(new Pair("me", "1000"));

        set.add(new Pair("and", "4000"));
        set.add(new Pair("you", "3000"));

        set.add(new Pair("food", "10000"));
        set.add(new Pair("hungry", "5000"));

        set.add(new Pair("later", "6000"));
        set.add(new Pair("myself", "1000"));

        for (Iterator i = set.iterator(); i.hasNext();)

            System.out.println(i.next());
    }
}

class Pair implements Comparable {
    private final String name;
    private final int number;

    public Pair(String name, int number) {
        this.name = name;
        this.number = number;
    }

    public Pair(String name, String number) throws NumberFormatException {
        this.name = name;
        this.number = Integer.parseInt(number);

    }

    public int compareTo(Object o) {
        if (o instanceof Pair) {
            int cmp = Double.compare(number, ((Pair) o).number);
            if (cmp != 0) {
                return cmp;
            }
            return name.compareTo(((Pair) o).name);
        }

        throw new ClassCastException("Cannot compare Pair with "
                + o.getClass().getName());

    }

    public String toString() {
        return name + ' ' + number;
    }
}

类似的C++代码:

typedef pair<string, int> PAIR;
int cmp(const PAIR& x, const PAIR& y)
{
    return x.second > y.second;
}

map<string,int> m;
vector<PAIR> vec;
for (map<wstring,int>::iterator curr = m.begin(); curr != m.end(); ++curr)
{
    vec.push_back(make_pair(curr->first, curr->second));
}
sort(vec.begin(), vec.end(), cmp);

上面方法的实质意义是:将Map结构中的键值对(Map.Entry)封装成一个自定义的类(结构),或者直接用
Map.Entry类。自定义类知道自己应该如何排序,也就是按值排序,具体为自己实现Comparable接口或构造一个Comparator对象,然后不用Map结构而采用有序集合(SortedSet, TreeSet是SortedSet的一种实现),这样就实现了Map中sort by value要达到的目的。就是说,不用Map,而是把Map.Entry当作一个对象,这样问题变为实现一个该对象的有序集合或对该对象的集合做排序。既可以用SortedSet,这样插入完成后自然就是有序的了,又或者用一个List或数组,然后再对其做排序(Collections.sort() or Arrays.sort())。

Encapsulate the information in its own class. Either implement
Comparable and write rules for the natural ordering or write a
Comparator based on your criteria. Store the information in a sorted
collection, or use the Collections.sort() method.

方法二:

You can also use the following code to sort by value:

public static Map sortByValue(Map map) {
        List list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {

            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getValue())
                        .compareTo(((Map.Entry) (o2)).getValue());

            }
        });
        Map result = new LinkedHashMap();

        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    public static Map sortByValue(Map map, final boolean reverse) {
        List list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {

            public int compare(Object o1, Object o2) {
                if (reverse) {
                    return -((Comparable) ((Map.Entry) (o1)).getValue())
                            .compareTo(((Map.Entry) (o2)).getValue());
                }
                return ((Comparable) ((Map.Entry) (o1)).getValue())
                        .compareTo(((Map.Entry) (o2)).getValue());
            }
        });

        Map result = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

        Map map = new HashMap();
        map.put("a", 4);
        map.put("b", 1);
        map.put("c", 3);
        map.put("d", 2);
        Map sorted = sortByValue(map);
        System.out.println(sorted);
// output : {b=1, d=2, c=3, a=4}
或者还可以这样:
Map map = new HashMap();
        map.put("a", 4);
        map.put("b", 1);
        map.put("c", 3);
        map.put("d", 2);

        Set<Map.Entry<String, Integer>> treeSet = new TreeSet<Map.Entry<String, Integer>>(
                new Comparator<Map.Entry<String, Integer>>() {
                    public int compare(Map.Entry<String, Integer> o1,
                            Map.Entry<String, Integer> o2) {
                        Integer d1 = o1.getValue();
                        Integer d2 = o2.getValue();
                        int r = d2.compareTo(d1);

                        if (r != 0)
                            return r;
                        else
                            return o2.getKey().compareTo(o1.getKey());
                    }

                });
        treeSet.addAll(map.entrySet());
        System.out.println(treeSet);
        // output : [a=4, c=3, d=2, b=1]

### Java 中对 Map 按照进行排序的方法 在 Java 中,可以使用自定义的 `Comparator` 来对 `Map` 的进行排序。以下是一个完整的代码示例,展示了如何根据 `Map` 的对其进行排序: ```java import java.util.*; public class MapSortByValueExample { public static void main(String[] args) { // 创建一个初始的 Map Map<String, Integer> map = new HashMap<>(); map.put("z", 10); map.put("b", 5); map.put("a", 6); map.put("c", 20); map.put("d", 1); map.put("e", 7); map.put("y", 8); map.put("n", 99); map.put("j", 50); map.put("m", 2); map.put("f", 9); System.out.println("Original Map:"); System.out.println(map); // 将 Map 转换为 List<Map.Entry> List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet()); // 使用 Comparator 对 List 进行排序(按升序) Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); // 将排序后的结果放入一个新的 LinkedHashMap 中以保持顺序 Map<String, Integer> sortedMap = new LinkedHashMap<>(); for (Map.Entry<String, Integer> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } System.out.println("Sorted Map by Value:"); System.out.println(sortedMap); } } ``` 上述代码中,首先将 `Map` 转换为 `List<Map.Entry>`,然后通过 `Collections.sort` 方法结合自定义的 `Comparator` 对列表进行排序[^2]。最后,将排序后的结果存储到一个 `LinkedHashMap` 中,以确保插入顺序得以保留。 此外,在 Java 8 中,可以利用流式操作简化这一过程: ```java import java.util.*; import java.util.stream.Collectors; public class MapSortByValueJava8Example { public static void main(String[] args) { // 创建一个初始的 Map Map<String, Integer> unsortMap = new HashMap<>(); unsortMap.put("z", 10); unsortMap.put("b", 5); unsortMap.put("a", 6); unsortMap.put("c", 20); unsortMap.put("d", 1); unsortMap.put("e", 7); unsortMap.put("y", 8); unsortMap.put("n", 99); unsortMap.put("j", 50); unsortMap.put("m", 2); unsortMap.put("f", 9); System.out.println("Original Map:"); System.out.println(unsortMap); // 使用 Java 8 的 Stream API 对 Map进行排序 Map<String, Integer> sortedMap = unsortMap.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new )); System.out.println("Sorted Map by Value:"); System.out.println(sortedMap); } } ``` 在这个 Java 8 示例中,使用了 `Stream` 和 `Collectors` 来实现排序[^3]。`Map.Entry.comparingByValue()` 提供了一个简洁的方式来比较 `Map` 的。 ### 注意事项 - 如果需要降序排序,可以在 `Comparator` 或 `comparingByValue()` 的基础上添加 `.reversed()`。 - 在处理大规模数据时,需注意性能问题,尤其是 `TreeMap` 的构造可能会导致额外开销。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值