基于键对hashmap进行排序 [英] Sorting hashmap based on keys

查看:46
本文介绍了基于键对hashmap进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 java 中有以下哈希图:

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. B0,060=0.0, B0609.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,B076=9.0,B00803.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}

感谢您的帮助!

推荐答案

使用排序的TreeMap:

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

它会自动将条目按键排序.我认为自然的 String 排序在您的情况下会很好.

It will automatically put entries sorted by keys. I think natural String ordering will be fine in your case.

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

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

这篇关于基于键对hashmap进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆