在 string[] 中存储排序后的 hashmap 的键值

发布于 2024-12-04 21:54:37 字数 721 浏览 0 评论 0原文

我很抱歉发布这个不清楚的问题。这是我第一次使用 hashmap,因此我很困惑。尝试在这里以更好的方式解释这个问题 - 将 hashmap 的键值存储在string[]

我有一个基于值排序的哈希图。我想从排序映射中提取键并将它们存储在 String[] 数组中。键的顺序(按值排序)很重要。 我使用此代码对值的哈希图进行排序 - http://www.xinotes.org/ Notes/note/306/

为了提取键数组,我尝试了

 String[] keys = (String[])( hm.keySet().toArray( new String[hm.size()] ) )

(这里hm是哈希图)

但是这个方法不起作用。键 string[] 有键,但不符合我想要的排序顺序。


更新: 我使用 linkedHashMap 并能够将排序后的键值存储在数组中。 这里是代码的链接。

I am sorry for posting this unclear question. This is my first time using hashmap and hence i was confused. Have tried to explain this question in a better way here - store key values of hashmap in string[]

I have a sorted hashmap based on values. I want to extract the keys from the sorted map and store them in a String[] array. The order of the keys(sorted by values) is important.
I used this code to sort the hashmap on values - http://www.xinotes.org/notes/note/306/

To extract the key array, I tried

 String[] keys = (String[])( hm.keySet().toArray( new String[hm.size()] ) )

(here hm is the hashmap)

But this method didnt work. The keys string[] has the keys but not in the sorted order I want.


Update:
I used linkedHashMap and was able to store the sorted key values in an array. Here is the link for the code.

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

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

发布评论

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

评论(5

长亭外,古道边 2024-12-11 21:54:37

根据拼写错误以及您正在使用哈希图的说明,键检索的顺序将与插入顺序不一致。为此,请使用 LinkedHashMap。这是使用您进行外部排序,然后将排序的条目插入到映射中。

如果您希望在将条目插入 Map 时对其进行排序,请使用 TreeMap。您可以使用自定义比较器或让您的关键对象实现 Comparable 接口。

Based on the typo, and your clarification that you are using hashmap, the order of the keys retrieval will not be consistent with the insert order. Use LinkedHashMap for that. This is using you do external sorting and then insert sorted entries into the map.

If you want the entries to be sorted while they are being inserted into the Map, use TreeMap. You can either use a custom comparator or make your key object implement Comparable interface.

嘿看小鸭子会跑 2024-12-11 21:54:37

HashMap 使用 key.hashValue() 对值进行排序。请改用 TreeMap。

HashMap uses key.hashValue() to sort the values. Use TreeMap instead.

(り薆情海 2024-12-11 21:54:37

我有一个基于值排序的哈希图。
我已经根据值对哈希图进行了排序

不,你还没有。 HashMap 根本没有排序。您可以获取 value() 作为集合,并且可以以任何您喜欢的方式对其进行排序,但它不会对 HashMap 本身进行排序。

但是这个方法并没有起作用。它以随机方式存储密钥。

它没有被定义为做任何不同的事情,特别是当你根本没有对 HashMap 进行排序时。

你需要澄清一下你在这里谈论的是什么。如果您想对值进行排序,请执行上述操作。如果您想对键进行排序,请使用 keys() 而不是 values()' 执行上述操作。如果您希望 Map 本身按键排序,请使用 TreeMap。如果您希望 Map 本身按值排序,那么运气不好,您不能。

I have a sorted hashmap based on values.
I have sorted a hashmap based on values

No you haven't. A HashMap isn't sorted at all. You can get the values(), as a Collection, and you can sort that any way you like, but it doesn't sort the HashMap itself.

But this method didnt work. It stores the keys in random fashion.

It isn't defined to do anything differently, especially as you haven't sorted the HashMap at all.

You need to clarify what you're talking about here. If you want to sort the values, do the above. If you want to sort the keys, do the above with keys() instead of values()'. If you want the Map itself sorted on keys, use a TreeMap. If you want the Map itself sorted on values, bad luck, you can't.

森罗 2024-12-11 21:54:37

试试这个:

public static void main(String[] args) {
    Map<String, String> hm = new TreeMap<String, String>();
    hm.put("AAA", "typeAAA");
    hm.put("BBB", "typeBBB");
    hm.put("ABB", "TypeABB");
    String[] keys = hm.keySet().toArray(new String[0]);
    for (String key : keys) {
        System.out.println("key: " + key);
    }
}

输出将是:

key: AAA
key: ABB
key: BBB

Try this:

public static void main(String[] args) {
    Map<String, String> hm = new TreeMap<String, String>();
    hm.put("AAA", "typeAAA");
    hm.put("BBB", "typeBBB");
    hm.put("ABB", "TypeABB");
    String[] keys = hm.keySet().toArray(new String[0]);
    for (String key : keys) {
        System.out.println("key: " + key);
    }
}

The output would be:

key: AAA
key: ABB
key: BBB
入画浅相思 2024-12-11 21:54:37

您似乎希望 HashMap 的键顺序与排序的键列表相同。这是根本不可能的HashMap 的键由哈希表算法确定;例如,一个复杂的过程,取决于键的哈希值以及插入和删除的顺序。

最接近的方法是创建一个 LinkedHashMap,并通过按排序键的顺序插入旧 HashMap 中的条目来填充它。如果您随后迭代 LinkedHashMap 的键,您将按照插入的顺序获取它们。但这是一个重量级的解决方案,如果您随后必须向“排序”映射添加更多条目,它就会崩溃。只使用 TreeMap 可能会更好。


我不想更改哈希图。我只想获取一个数组,其中键按排序值的顺序排列。

在这种情况下,您只需将 HashMap 的键提取到数组中并对其进行排序。代码在其他答案里已经给出了。

另一方面,如果您想做一些事情,以便地图的键始终按排序顺序出现(您似乎在其他评论中这么说),那么您正在更改地图。

It would seem that you want the order of the keys of a HashMap to be the same as your sorted list of keys. This is simply not possible. A HashMaps keys are determined by the hash table algorithms; e.g. a complicated process that depends on the keys' hash values and sequence of insertions and deletions.

The closest you will get is to create a LinkedHashMap, and populate it by inserting entries from the old HashMap in the order of the sorted keys. If you then iterate over the LinkedHashMap's keys, you will get them back in the order in which they were inserted. But this is a heavy-weight solution, and it breaks down if you subsequently have to add more entries to the "sorted" map. It may be better to just use a TreeMap.


I dont want to make changes to the hashmap. I just want to get an array with the keys in the order of sorted values.

In that case, you simply need to extract the HashMap's keys into an array and sort it. The code has been given in other answers.

On the other hand, if you want to do something so that the map's keys always come out in the sorted order (which you seem to be saying in other comments), you are changing the map.

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