How to use SortedMap interface in Java?

前端 未结 6 623
谎友^
谎友^ 2020-12-01 02:03

I have a

 Map

What is the best way to keep the map sorted according to the float?

Is SortedMap

6条回答
  •  执念已碎
    2020-12-01 02:22

    TreeMap sorts by the key natural ordering. The keys should implement Comparable or be compatible with a Comparator (if you passed one instance to constructor). In you case, Float already implements Comparable so you don't have to do anything special.

    You can call keySet to retrieve all the keys in ascending order.

提交回复
热议问题