日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

[Java]HashMap的两种排序方式

發布時間:2023/12/31 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 [Java]HashMap的两种排序方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先將Map中的key和value全部取出來封裝成JavaBea數組,再將這個數組排序,
排序完成后,重新寫回Map中,寫回時采用LinkedHashMap可以保證迭代的順序。

下面的代碼可以參考一下:

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

public class Test {

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("ee", 3);
        map.put("b", 1);
        map.put("d", 2);
        map.put("eee", 3);
        map.put("A", 1);
        map.put("K", 2);
        map.put("ade", 1);
        map.put("c", 2);
        map.put("aee", 3);
        map.put("a", 1);
        map.put("faed", 2);
        map.put("bdd", 1);
        map.put("qec", 2);
        map.put("eade", 3);
        map.put("Aadf", 1);
        map.put("Kqe", 2);

        Map<String, Integer> sortMap = new Test().sortMap(map);

        for(Map.Entry<String, Integer> entry : sortMap.entrySet()) {
            System.out.println(entry.getKey() + " --> " + entry.getValue());
        }
    }

    public <K, V extends Number> Map<String, V> sortMap(Map<String, V> map) {
        class MyMap<M, N> {
            private M key;
            private N value;
            private M getKey() {
                return key;
            }
            private void setKey(M key) {
                this.key = key;
            }
            private N getValue() {
                return value;
            }
            private void setValue(N value) {
                this.value = value;
            }
        }

        List<MyMap<String, V>> list = new ArrayList<MyMap<String, V>>();
        for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
            MyMap<String, V> my = new MyMap<String, V>();
            String key = i.next();
            my.setKey(key);
            my.setValue(map.get(key));
            list.add(my);
        }

        Collections.sort(list, new Comparator<MyMap<String, V>>() {
            public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {
                if(o1.getValue() == o2.getValue()) {
                    return o1.getKey().compareTo(o2.getKey());
                }else{
                    return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());
                }
            }
        });

        Map<String, V> sortMap = new LinkedHashMap<String, V>();
        for(int i = 0, k = list.size(); i < k; i++) {
            MyMap<String, V> my = list.get(i);
            sortMap.put(my.getKey(), my.getValue());
        }
        return sortMap;
    }
}

  

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("d", 2);
map.put("c", 1);
map.put("b", 1);
map.put("a", 3);

List<Map.Entry<String, Integer>> infoIds =
new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

//排序前
for (int i = 0; i < infoIds.size(); i++) {
String id = infoIds.get(i).toString();
System.out.println(id);
}
//d 2
//c 1
//b 1
//a 3

//排序
Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
//return (o2.getValue() - o1.getValue());
return (o1.getKey()).toString().compareTo(o2.getKey());
}
});

//排序后
for (int i = 0; i < infoIds.size(); i++) {
String id = infoIds.get(i).toString();
System.out.println(id);
}
//根據key排序
//a 3
//b 1
//c 1
//d 2
//根據value排序
//a 3
//d 2
//b 1
//c 1

參考: http://bbs.csdn.net/topics/230054066

    http://www.cnblogs.com/lovebread/archive/2009/11/23/1609121.html

總結

以上是生活随笔為你收集整理的[Java]HashMap的两种排序方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。