简体   繁体   中英

How to sort the HashMap<String , Byte> by value in Android?

I am developing in Android , and I want to sort the the HashMap<String , Byte> . It print the all value like the following code and log:

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

}

And the log show like the following:

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

How to sort the HashMap by value?

Thanks in advance.

  1. Pass your Hashmap to a Treemap
  2. Use the Comparator class to define your ordering

Have a look here Ordering HashMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM