根据密钥对散列图进行排序 [英] Sorting hashmap based on keys
问题描述
{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}
blockquote>
我应该如何对散列图进行排序,以便将字母表和数字后面的数字考虑在内?
产生的哈希映射应该如下所示:
lockquote
欣赏帮助!
使用排序后的 / code>:
地图< String,Float> map = new TreeMap<>(yourMap);
它会自动放入按键排序的条目。我认为自然的 String
排序在你的情况下可以。
$ b
请注意 HashMap
由于查找优化不会保持顺序。
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!
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.
这篇关于根据密钥对散列图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!