How to use the SortedMap interface in Java?

I have

Map<Float, MyObject> 

What is the best way to keep a map sorted by float?

Is SortedMap best answer? TreeMap ? How do i use this?

I create a map only once and often myMap.put() MyObject using myMap.put() and myMap.get() .

+61
java sortedmap
Sep 15 '11 at 8:14
source share
5 answers

I would use TreeMap , which implements SortedMap . It is designed specifically for this.

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 is a list of tutorials related to TreeMap.

+81
Sep 15 '11 at 8:17
source share

TreeMap is probably the easiest way to do this. You use it just like a regular card.

t

  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 find out what else you can do with it.

+41
Sep 15 '11 at 8:21
source share

You can use TreeMap, which internally implements SortedMap, below is an example

Sort Ascending:

  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()); } 

Sort Descending:

If you always want the top-down order to be used when creating the map, in general, if you need only once, create a TreeMap with the top-down order and put all the data from the original map.

  // 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 more information about SortedMAP read http://examples.javacodegeeks.com/core-java/util/treemap/java-sorted-map-example/

+13
Sep 08 '15 at 19:14
source share

TreeMap, which is an implementation of the SortedMap interface, will work.

How to use it?

 Map<Float, MyObject> map = new TreeMap<Float, MyObject>(); 
+3
Sep 15 '11 at 8:16
source share

TreeMap sorts by natural ordering. Keys must implement Comparable or be compatible with Comparator (if you passed one instance to the constructor). In this case, Float already implements Comparable , so you don’t have to do anything.

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

+2
Sep 15 '11 at 8:23
source share



All Articles