java 对map或hashmap排序的两种方法

Map 这个在java 程序中最常见的类型,通常使用的是其具体的实现 hashmap 来定义数据。但存储在hashmap里面的数据,经常会根据需要,进行排序处理,在这里总结了两种方法来排序,如果以后发现其他的,继续增加
1. 根据 map 的 key 值来排序 ( 利用treemap 特性实现 )
2. 根据 map 的 value 值来排序( 利用 list 特性实现 )


下面是两种处理办法的代码
第一种办法,根据key 排序
程序代码 程序代码

package test;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class MapOrderByKey {
    public static void main(String[] args) {
        Map<String, String> unsortMap = new HashMap<String, String>();
        unsortMap.put("2", "B");
        unsortMap.put("1", "A");
        unsortMap.put("4", "D");
        unsortMap.put("3", "B");
        unsortMap.put("7", "C");
        unsortMap.put("5", "z");
        unsortMap.put("6", "b");
        unsortMap.put("8", "a");

        System.out.println("Unsort Map......");
        printMap(unsortMap);
        System.out.println("Sorted Map......");
        Map<String, String> treeMap = new TreeMap<String, String>(unsortMap);
        printMap(treeMap);
    }

    public static void printMap(Map<String, String> map) {
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() + " Value : "
                + entry.getValue());
        }
    }

}

运行之后,你会发现,输出结果按照 key 的顺序排列了,说明这种方法可行.
结果如下:
程序代码 程序代码

Unsort Map......
Key : 3 Value : B
Key : 2 Value : B
Key : 1 Value : A
Key : 7 Value : C
Key : 6 Value : b
Key : 5 Value : z
Key : 4 Value : D
Key : 8 Value : a
Sorted Map......
Key : 1 Value : A
Key : 2 Value : B
Key : 3 Value : B
Key : 4 Value : D
Key : 5 Value : z
Key : 6 Value : b
Key : 7 Value : C
Key : 8 Value : a


第二种办法,根据value 进行排序
程序代码 程序代码

package test;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MapOrderByValue {

    public static void main(String[] args) {

        Map<String, String> unsortMap = new HashMap<String, String>();
        unsortMap.put("2", "B");
        unsortMap.put("1", "A");
        unsortMap.put("4", "D");
        unsortMap.put("3", "B");
        unsortMap.put("7", "C");
        unsortMap.put("5", "z");
        unsortMap.put("6", "b");
        unsortMap.put("8", "a");

        System.out.println("Unsort Map......");
        printMap(unsortMap);

        System.out.println("Sorted Map......");
        Map<String, String> sortedMap = sortByComparator(unsortMap);
        printMap(sortedMap);

    }

    private static Map sortByComparator(Map unsortMap) {
        List list = new LinkedList(unsortMap.entrySet());
        // sort list based on comparator
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getValue())
                                       .compareTo(((Map.Entry) (o2)).getValue());
            }
        });

        // put sorted list into map again
                //LinkedHashMap make sure order in which keys were inserted
        Map sortedMap = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }

    public static void printMap(Map<String, String> map){
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey()
                                   + " Value : " + entry.getValue());
        }
    }
}


运行,你会发现,后来的结果是按照value来排序的。在仔细看代码,你会发现,其实现的一个基本原理就是将 map 转为 list ,然后利用Comparator 对 list 进行排序,最后再把排好序的值放入 map 里面。这样就实现了在map里面对value 进行排序.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值