使用重复值对HashMap进行排序

时间:2012-10-05 01:16:57

标签: java sorting hashmap

您好我使用以下代码对我的HashMap进行排序,它正确排序地图但不计算重复值,

Map<String, Integer> mymap = new HashMap<String, Integer>();
mymap.put("item1", 5);
mymap.put("item2", 1);
mymap.put("item3", 7);
mymap.put("item4", 1);

Map<String, Integer> tempMap = new HashMap<String, Integer>();
for (String wsState : mymap.keySet()) {
    tempMap.put(wsState, mymap.get(wsState));
}

List<String> mapKeys = new ArrayList<String>(tempMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(tempMap.values());
HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i = 0; i < size; i++) {
    sortedMap.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),
            (Integer) sortedArray[i]);
}
for (Map.Entry<String, Integer> entry : mymap.entrySet())
    System.out.println("Item is:" + entry.getKey() + " with value:"
            + entry.getValue());

System.out.println("***");

for (Map.Entry<String, Integer> entry : sortedMap.entrySet())
    System.out.println("Item is:" + entry.getKey() + " with value:"
            + entry.getValue());

结果如下(项目4未显示,因为其值与item2的相同!!!):

Item is:item4 with value:1
Item is:item2 with value:1
Item is:item3 with value:7
Item is:item1 with value:5
***
Item is:item2 with value:1
Item is:item1 with value:5
Item is:item3 with value:7

它是一个HashMap,需要按值排序。 预期输出为:

Item is:item3 with value:7
Item is:item1 with value:5
Item is:item2 with value:1
Item is:item4 with value:1

OR

Item is:item2 with value:1
Item is:item4 with value:1
Item is:item1 with value:5
Item is:item3 with value:7

3 个答案:

答案 0 :(得分:10)

您正在使用TreeSet<Integer> sortedSet

根据定义,SETS不允许重复。

以下是一个示例,即按预期排序,而不会丢失任何条目。

import java.util.*;

public class Test {

public static Map<String, Integer> sortByValueDesc(Map<String, Integer> map) {
    List<Map.Entry<String, Integer>> list = new LinkedList(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o2.getValue().compareTo(o1.getValue());
        }
    });

    Map<String, Integer> result = new LinkedHashMap<>();
    for (Map.Entry<String, Integer> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

    public static void main(String[] args) {

        HashMap<String, Integer> map = new HashMap<String, Integer>();

        map.put("item1", 1);
        map.put("item2", 2);
        map.put("item3", 1);
        map.put("item4", 7);
        map.put("item5", 3);
        map.put("item6", 4);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("Item is:" + entry.getKey() + " with value:"
                    + entry.getValue());
        }

        System.out.println("*******");

        Map<String,Integer> sortedMap = sortByValueDesc(map);

        for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
            System.out.println("Item is:" + entry.getKey() + " with value:"
                    + entry.getValue());
        }

    }

}

我得到的结果是(现在我检查你想要更大的值是第一个):

Item is:item4 with value:7
Item is:item2 with value:2
Item is:item3 with value:1
Item is:item1 with value:1
Item is:item6 with value:4
Item is:item5 with value:3
*******
Item is:item4 with value:7
Item is:item6 with value:4
Item is:item5 with value:3
Item is:item2 with value:2
Item is:item3 with value:1
Item is:item1 with value:1

为什么你在这里失去一个元素是你的问题:

//HERE YOU ARE GETTING ALL THE VALUES
List<Integer> mapValues = new ArrayList<Integer>(tempMap.values());
HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();

//YOU ARE INSERTING THE VALUES TO A TreeSet WHICH WILL REMOVE DUPLICATES
TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);

答案 1 :(得分:1)

我相信你需要treeMap。它不允许重复,并且可以作为密钥进行比较。根据compareTo()进行排序,如果它是可比较的,如果没有,则在构造函数中询问比较器。地图不允许重复

    Map<String,Integer> mymap = new TreeMap<String,Integer>();
    mymap.put("item1", 5);
    mymap.put("item2", 1);
    mymap.put("item3", 7);
    mymap.put("item4", 1);

for(Map.Entry<String, Integer> entry: mymap.entrySet())
            System.out.println("Item is:" + entry.getKey() + " with value:" + 
                    entry.getValue());

答案 2 :(得分:0)

public static void main(String[] args) {
    Set<Item> s = new TreeSet<Item>();
    s.add(new Item("item1", 5));
    s.add(new Item("item2", 1));
    s.add(new Item("item3", 7));
    s.add(new Item("item4", 1));

    for (Item it : s) {
        System.out.println("Item is:" + it.getName() + " with value:"
                + it.getValue());
    }

}

class Item implements Comparable<Item> {
    private Integer value;
    private String name;

    Item(String name, int val) {
        this.name = name;
        this.value = val;
    }

    // getters and sets

    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (obj.getClass() != getClass())
            return false;

        Item i = (Item) obj;
        if (i.getValue().equals(getValue())
                && i.getName().equals(getName()))
            return true;
        return false;

    }

    public int compareTo(Item o) {
        return getValue().compareTo(o.getValue());
    }

    public boolean equals(Object o) {
        return false;
    }
}