根据键对哈希图进行排序

最后发布: 2011-10-22 16:14:09


问题

I have the following hashmap in java: 我在Java中具有以下哈希图:

{B046=0.0, A061=3.0, A071=0.0, B085=0.0, B075=3.0, B076=9.0, B086=3.0, B095=0.0, B096=0.0, A052=0.0, B066=0.0, B056=9.0, B065=0.0, B055=9.0} {B046 = 0.0,A061 = 3.0,A071 = 0.0,B085 = 0.0,B075 = 3.0,B076 = 9.0,B086 = 3.0,B095 = 0.0,B096 = 0.0,A052 = 0.0,B066 = 0.0,B056 = 9.0,B065 = 0.0,B055 = 9.0}

How should I go about sorting the hashmap such that the Alphabet, followed by the numerical figures are taken into account? 我应该如何对哈希图进行排序,以便考虑字母和数字?

The resulting hashmap should look like this: 生成的哈希图应如下所示:

{A052=0.0,A061=3.0,A071=0.0,B046=0.0,B055=9.0,B056=9.0,B065=0.0,B066=0.0,B075=3.0,B076=9.0,B085=0.0,B086=3.0,B095=0.0,B096=0.0} {A052 = 0.0,A061 = 3.0,A071 = 0.0,B046 = 0.0,B055 = 9.0,B056 = 9.0,B065 = 0.0,B066 = 0.0,B075 = 3.0,B076 = 9.0,B085 = 0.0,B086 = 3.0,B095 = 0.0,B096 = 0.0}

Appreciate the help! 感谢帮助!

java hashmap
回答

Use sorted TreeMap : 使用排序的TreeMap

Map<String, Float> map = new TreeMap<>(yourMap);

It will automatically put entries sorted by keys. 它将自动放置按键排序的条目。 I think natural String ordering will be fine in your case. 我认为自然String排序将适合您的情况。

Note that HashMap due to lookup optimizations does not preserve order. 请注意,由于查找优化, HashMap不会保留顺序。


回答

Use a TreeMap with a custom comparator. 将TreeMap与自定义比较器一起使用。

class MyComparator implements Comparator<String>
    {
        public int compare(String o1,String o2)
        {
            // Your logic for comparing the key strings
        }
    }

TreeMap<String, Float> tm = new TreeMap<String , Float>(new MyComparator());

As you add new elements, they will be automatically sorted. 添加新元素时,它们将自动排序。

In your case, it might not even be necessary to implement a comparator because String ordering might be sufficient. 在您的情况下,甚至不需要实现比较器,因为字符串排序可能就足够了。 But if you want to implement special cases, like lower case alphas appear before upper case, or treat the numbers a certain way, use the comparator. 但是,如果要实现特殊情况,例如小写字母在大写字母之前出现,或以某种方式处理数字,请使用比较器。


回答

TreeMap is your best bet for these kind of sorting (Natural). TreeMap是您进行此类排序(自然)的最佳选择。 TreeMap naturally sorts according to the keys. TreeMap自然根据键进行排序。

HashMap does not preserve insertion order nor does it sort the map. HashMap不会保留插入顺序,也不会对地图进行排序。 LinkedHashMap keeps the insertion order but doesn't sort the map automatically. LinkedHashMap保留插入顺序,但不会自动对地图排序。 Only TreeMap in the Map interface sorts the map according to natural order (Numerals first, upper-case alphabet second, lower-case alphabet last). 只有Map界面中的TreeMap根据自然顺序对地图进行排序(数字首先,小写字母第二,小写字母最后)。


回答

使用TreeMap ,尽管让地图看起来“有点像”有点模糊-您也可以根据条件对键进行排序,然后遍历地图,以获取每个对象。


回答

Using the TreeMap you can sort the Map. 使用TreeMap可以对地图进行排序。

Map<String, String> map = new HashMap<String, String>();        
Map<String, String> treeMap = new TreeMap<String, String>(map);
//show hashmap after the sort
for (String str : treeMap.keySet()) {
    System.out.println(str);
}


回答

您可以使用TreeMap将以排序形式存储值。

Map <String, String> map = new TreeMap <String, String>();


回答

Just use a TreeMap . 只需使用TreeMap It implements the SortedMap interface, and thus automatically sorts the keys it contains. 它实现了SortedMap接口,从而自动对它包含的键进行排序。 Your keys can just be sorted alphabetically to get the desired result, so you don't even need to provide a comparator. 您可以按字母顺序对键进行排序以获得所需的结果,因此您甚至不需要提供比较器。

HashMaps are never sorted. HashMaps从未排序。 The only thing you coulkd do with a HashMap is get all the keys, and store them in a sorted set or in a List and sort the List. 您对HashMap唯一要做的就是获取所有密钥,并将它们存储在排序集中或列表中,然后对列表进行排序。


回答

TreeMap will automatically sort in ascending order. TreeMap将自动按升序排序。 If you want to sort in descending order, use the following code: 如果要按降序排序,请使用以下代码:

Copy the below code within your class and outside of the main execute method: 将以下代码复制到您的类内和主要execute方法之外:

static class DescOrder implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {      
        return o2.compareTo(o1);
    }
    }

Then in your logic: 然后按照您的逻辑:

TreeMap<String, String> map = new TreeMap<String, String>(new DescOrder());
map.put("A", "test1");
map.put("C", "test3");
map.put("E", "test5");
map.put("B", "test2");
map.put("D", "test4");