How to use SortedMap interface in Java?
问题 I have a Map<Float, MyObject> What is the best way to keep the map sorted according to the float? Is SortedMap the best answer? TreeMap ? How do I use it? I only create the map once and replace the MyObject frequently using myMap.put() and myMap.get() . 回答1: I would use TreeMap , which implements SortedMap . It is designed exactly for that. Example: Map<Integer, String> map = new TreeMap<Integer, String>(); // Add Items to the TreeMap map.put(1, "One"); map.put(2, "Two"); map.put(3, "Three");