在HashMap中,怎样先按value排序,再按key排序 (转)

来源:互联网 发布:cst电磁仿真软件2016 编辑:程序博客网 时间:2024/06/02 07:10

例如:Map<String,Integer> result = new HashMap<String,Integer>();
先按数字排序,再按字母排序。把HashMap中的key-value 放到TreeMap
中再排序行吗?最好给个例子。

先将 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;
    }
}

----------------------------------------------------------------------------------------------------------------------------------------

return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());

返回 两个对象间 value 的比较结果,比如,一个是 5,一个是 3,那它们的差就是 2,即 2 > 0 的,
因此 5 就排在了 3 的后面。如果是小于 0 的,就会排在前面,等于 0 时,我们就去比较 key 的大小。

这样的排序规则应该是你想要的,呵呵。

我在 12 楼贴的代码有点问题,改正一下:

         Collections.sort(list, new Comparator<MyMap<String, V>>() {
           
public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {
               
if(o1.getValue().equals(o2.getValue())) {         // 改成这一行
                    return o1.getKey().compareTo(o2.getKey());
                }
else{
                   
return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());
                }
            }
        });