How to sort a treemap based on its values?

前端 未结 9 1013
萌比男神i
萌比男神i 2020-12-01 04:53

How can I sort a treemap using its values rather than the key?

9条回答
  •  攒了一身酷
    2020-12-01 05:23

    You cannot as the TreeMap's comparator is run against the keys only, e.g. see this constructor.

    Anyway, you can use multiple Collections, use the TreeMap (or rather HashMap) for looking up elements by keys, and have a SortedSet to iterate on the values.

提交回复
热议问题