问题
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");
// Iterate over them
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
See the Java tutorial page for SortedMap.
And here a list of tutorials related to TreeMap.
回答2:
A TreeMap is probably the most straightforward way of doing this. You use it exactly like a normal Map.
ie
Map<Float,String> mySortedMap = new TreeMap<Float,MyObject>();
// Put some values in it
mySortedMap.put(1.0f,"One");
mySortedMap.put(0.0f,"Zero");
mySortedMap.put(3.0f,"Three");
// Iterate through it and it'll be in order!
for(Map.Entry<Float,String> entry : mySortedMap.entrySet()) {
System.out.println(entry.getValue());
} // outputs Zero One Three
It's worth taking a look at the api docs, http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html to see what else you can do with it.
回答3:
You can use TreeMap which internally implements the SortedMap below is the example
Sorting by ascending ordering :
Map<Float, String> ascsortedMAP = new TreeMap<Float, String>();
ascsortedMAP.put(8f, "name8");
ascsortedMAP.put(5f, "name5");
ascsortedMAP.put(15f, "name15");
ascsortedMAP.put(35f, "name35");
ascsortedMAP.put(44f, "name44");
ascsortedMAP.put(7f, "name7");
ascsortedMAP.put(6f, "name6");
for (Entry<Float, String> mapData : ascsortedMAP.entrySet()) {
System.out.println("Key : " + mapData.getKey() + "Value : " + mapData.getValue());
}
Sorting by descending ordering :
If you always want this create the map to use descending order in general, if you only need it once create a TreeMap with descending order and put all the data from the original map in.
// Create the map and provide the comparator as a argument
Map<Float, String> dscsortedMAP = new TreeMap<Float, String>(new Comparator<Float>() {
@Override
public int compare(Float o1, Float o2) {
return o2.compareTo(o1);
}
});
dscsortedMAP.putAll(ascsortedMAP);
for further information about SortedMAP read http://examples.javacodegeeks.com/core-java/util/treemap/java-sorted-map-example/
回答4:
TreeMap, which is an implementation of the SortedMap interface, would work.
How do I use it ?
Map<Float, MyObject> map = new TreeMap<Float, MyObject>();
回答5:
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.
来源:https://stackoverflow.com/questions/7427758/how-to-use-sortedmap-interface-in-java