根据键对 hashmap 进行排序
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用TreeMap(构造函数):
使用TreeMap(PutAll方法):
Map接口的实现:
Use TreeMap (Constructor):
Use TreeMap (PutAll method):
Implementation of Map interface:
使用 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.
只需使用
TreeMap
。它实现了SortedMap
接口,从而自动对其包含的键进行排序。您只需按字母顺序对键进行排序即可获得所需的结果,因此您甚至不需要提供比较器。HashMap 永远不会排序。使用 HashMap 唯一能做的就是获取所有键,并将它们存储在排序集或列表中,并对列表进行排序。
Just use a
TreeMap
. It implements theSortedMap
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.
使用TreeMap,您可以对地图进行排序。
Using the TreeMap you can sort the Map.
您可以使用
TreeMap
它将以排序的形式存储值。You can use
TreeMap
which will store values in sorted form.TreeMap 会自动按升序排序。如果要按降序排序,请使用以下代码:
在类内和主执行方法之外复制以下代码:
然后在您的逻辑中:
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:
Then in your logic:
使用排序的
TreeMap
:它会自动将条目按键排序。我认为自然的
String
排序适合您的情况。请注意,由于查找优化,
HashMap
不会保留顺序。Use sorted
TreeMap
: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.使用带有自定义比较器的 TreeMap。
当您添加新元素时,它们将自动排序。
在您的情况下,甚至可能不需要实现比较器,因为字符串排序可能就足够了。但是,如果您想实现特殊情况,例如小写字母出现在大写字母之前,或者以某种方式处理数字,请使用比较器。
Use a TreeMap with a custom comparator.
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
是此类排序的最佳选择(自然)。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. OnlyTreeMap
in theMap
interface sorts the map according to natural order (Numerals first, upper-case alphabet second, lower-case alphabet last).