java - 对具有重复值的 HashMap 进行排序

标签 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());

结果如下(item 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

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

最佳答案

您正在使用 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);

关于java - 对具有重复值的 HashMap 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12738216/

相关文章:

java - 值聚类

java - 如何在Java中使用数组

java - Java中如何查看一个数组是否包含另一个数组的所有元素?

java - 通过 Maven 添加 DB2 JDBC 驱动程序

sql - Redis 在多个键上的复杂排序。构建事件提要

java - 如何在 switch case 中使用 Map 中的 int 值?

java - Java 中 HashMap 中一个键的多个值

java - 如何在使用比较器接口(interface)时获取矩形对象的数组列表以显示每个矩形的面积

c# - 对嵌套集合使用 FILO

objective-c - NSDictionary 键按数值排序