如何在Android中按值对HashMap <string,=“”byte =“”>进行排序?

时间:2015-10-13 02:57:25

标签: java android hashmap

我正在开发Android,我想对HashMap<String , Byte>进行排序。它打印所有值,如下面的代码和日志:

for (Object key : groups.keySet()) {
    Log.d("addGroup", "groups.get(key) = " + groups.get(key) + " , key = " + key);

}

日志显示如下:

groups.get(key) = -6 , key = iii
groups.get(key) = -5 , key = qqq
groups.get(key) = -3 , key = bbb
groups.get(key) = -7 , key = xxd
groups.get(key) = -2 , key = kmn
groups.get(key) = -4 , key = hhh
groups.get(key) = -12 , key = yhn
groups.get(key) = -9 , key = 987
groups.get(key) = -13 , key = zxc
groups.get(key) = -10 , key = hxt
groups.get(key) = -11 , key = tfx

如何按值对HashMap进行排序?

提前致谢。

1 个答案:

答案 0 :(得分:1)

  1. 将您的Hashmap传递给Treemap
  2. 使用Comparator类定义您的订购
  3. 在这里查看Ordering HashMap