根据键对 hashmap 进行排序

发布于 2024-12-11 12:01:37 字数 429 浏览 0 评论 0原文

我在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}

我应该如何对哈希图进行排序,以便考虑字母表和后面的数字?

生成的哈希图应如下所示:

{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}

感谢您的帮助!

I have the following hashmap in 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}

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}

Appreciate the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

尹雨沫 2024-12-18 12:01:38

使用TreeMap(构造函数):

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

使用TreeMap(PutAll方法):

Map<String, Float> sortedMap = new TreeMap<>();
sortedMap.putAll(yourMap);

Map接口的实现:

  1. TreeMap - 自动排序插入时按键按升序排列。
  2. HashMap - 不会维持插入顺序。
  3. LinkedHashMap - 将保持插入顺序。

Use TreeMap (Constructor):

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

Use TreeMap (PutAll method):

Map<String, Float> sortedMap = new TreeMap<>();
sortedMap.putAll(yourMap);

Implementation of Map interface:

  1. TreeMap - Automatically sort the keys in ascending order while inserting.
  2. HashMap - Order of insertion won't be maintained.
  3. LinkedHashMap - Order of insertion will be maintained.
孤者何惧 2024-12-18 12:01:38

使用 TreeMap,尽管有地图“看起来像那样”有点模糊 - 您也可以根据您的标准对键进行排序并迭代地图,检索每个对象。

Use a TreeMap, although having a map "look like that" is a bit nebulous--you could also just sort the keys based on your criteria and iterate over the map, retrieving each object.

葬﹪忆之殇 2024-12-18 12:01:38

只需使用TreeMap。它实现了 SortedMap 接口,从而自动对其包含的键进行排序。您只需按字母顺序对键进行排序即可获得所需的结果,因此您甚至不需要提供比较器。

HashMap 永远不会排序。使用 HashMap 唯一能做的就是获取所有键,并将它们存储在排序集或列表中,并对列表进行排序。

Just use a TreeMap. It implements the SortedMap interface, and thus automatically sorts the keys it contains. 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. 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.

太傻旳人生 2024-12-18 12:01:38

使用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);
}

Using the TreeMap you can sort the Map.

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);
}
如何视而不见 2024-12-18 12:01:38

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

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

You can use TreeMap which will store values in sorted form.

Map <String, String> map = new TreeMap <String, String>();
寒江雪… 2024-12-18 12:01:38

TreeMap 会自动按升序排序。如果要按降序排序,请使用以下代码:

在类内和主执行方法之外复制以下代码:

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

然后在您的逻辑中:

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");

TreeMap will automatically sort in ascending order. 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:

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");
纸短情长 2024-12-18 12:01:37

使用排序的TreeMap

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

它会自动将条目按键排序。我认为自然的 String 排序适合您的情况。

请注意,由于查找优化,HashMap 不会保留顺序。

Use sorted 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.

Note that HashMap due to lookup optimizations does not preserve order.

同展鸳鸯锦 2024-12-18 12:01:37

使用带有自定义比较器的 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());

当您添加新元素时,它们将自动排序。

在您的情况下,甚至可能不需要实现比较器,因为字符串排序可能就足够了。但是,如果您想实现特殊情况,例如小写字母出现在大写字母之前,或者以某种方式处理数字,请使用比较器。

Use a TreeMap with a custom comparator.

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.

舂唻埖巳落 2024-12-18 12:01:37

TreeMap 是此类排序的最佳选择(自然)。 TreeMap 自然地根据键进行排序。

HashMap 不保留插入顺序,也不对映射进行排序。 LinkedHashMap 保留插入顺序,但不会自动对映射进行排序。只有Map接口中的TreeMap按照自然顺序对地图进行排序(数字在前,大写字母在后,小写字母在后)。

TreeMap is your best bet for these kind of sorting (Natural). TreeMap naturally sorts according to the keys.

HashMap does not preserve insertion order nor does it sort the map. LinkedHashMap keeps the insertion order but doesn't sort the map automatically. Only TreeMap in the Map interface sorts the map according to natural order (Numerals first, upper-case alphabet second, lower-case alphabet last).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文