如何在Java中使用SortedMap接口?

时间:2022-09-06 12:05:31

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?

SortedMap是最好的答案吗? TreeMap的?我该如何使用它?

I only create the map once and replace the MyObject frequently using myMap.put() and myMap.get().

我只创建一次地图并使用myMap.put()和myMap.get()经常替换MyObject。

5 个解决方案

#1


63  

I would use TreeMap, which implements SortedMap. It is designed exactly for that.

我会使用TreeMap,它实现了SortedMap。它专为此而设计。

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.

请参阅SortedMap的Java教程页面。这里有一个与TreeMap相关的教程列表。

#2


36  

A TreeMap is probably the most straightforward way of doing this. You use it exactly like a normal Map.

TreeMap可能是最简单的方法。你使用它就像普通的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.

值得一看的是api docs,http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html,看看你还能用它做些什么。

#3


9  

You can use TreeMap which internally implements the SortedMap below is the example

你可以使用TreeMap在内部实现下面的SortedMap就是例子

Sorting by ascending ordering :

按升序排序:

  Map<Integer,String> ascsortedMAP = new TreeMap<Integer,String>();

  ascsortedMAP.put(8, "name8");
  ascsortedMAP.put(5, "name5");
  ascsortedMAP.put(15, "name15");
  ascsortedMAP.put(35, "name35");
  ascsortedMAP.put(44, "name44");
  ascsortedMAP.put(7, "name7");
  ascsortedMAP.put(6, "name6"); 

  for(Map.Entry<Integer, String> mapData : ascsortedMAP.entrySet()) {
    System.out.println("Key : " +mapData.getKey()+ "Value : "+mapData.getValue());
    }

Sorting by descending ordering :

按降序排序:

  //Create the map and provide the comparator as a argument
  Map<Integer,String> dscsortedMAP = new TreeMap<Integer,String>(new Comparator<Integer>() 
     {
        @Override
        public int compare(Integer o1, Integer o2) {                
            return o2.compareTo(o1);
         }
     });
    dscsortedMAP.putAll(ascsortedMAP);

      for(Map.Entry<Integer, String> mapData : dscsortedMAP.entrySet()) {
        System.out.println("Key : " +mapData.getKey()+" Value : "+mapData.getValue());
        }

for further information about SortedMAP read http://examples.javacodegeeks.com/core-java/util/treemap/java-sorted-map-example/

有关SortedMAP的更多信息,请阅读http://examples.javacodegeeks.com/core-java/util/treemap/java-sorted-map-example/

#4


3  

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

TreeMap是SortedMap接口的一个实现,可以工作。

How do I use it ?

我该如何使用它?

Map<Float, MyObject> map = new TreeMap<Float, MyObject>();

#5


2  

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.

TreeMap按主要自然顺序排序。键应该实现Comparable或与Comparator兼容(如果您将一个实例传递给构造函数)。在你的情况下,Float已经实现了Comparable,所以你不需要做任何特别的事情。

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

您可以调用keySet以按升序检索所有键。

#1


63  

I would use TreeMap, which implements SortedMap. It is designed exactly for that.

我会使用TreeMap,它实现了SortedMap。它专为此而设计。

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.

请参阅SortedMap的Java教程页面。这里有一个与TreeMap相关的教程列表。

#2


36  

A TreeMap is probably the most straightforward way of doing this. You use it exactly like a normal Map.

TreeMap可能是最简单的方法。你使用它就像普通的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.

值得一看的是api docs,http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html,看看你还能用它做些什么。

#3


9  

You can use TreeMap which internally implements the SortedMap below is the example

你可以使用TreeMap在内部实现下面的SortedMap就是例子

Sorting by ascending ordering :

按升序排序:

  Map<Integer,String> ascsortedMAP = new TreeMap<Integer,String>();

  ascsortedMAP.put(8, "name8");
  ascsortedMAP.put(5, "name5");
  ascsortedMAP.put(15, "name15");
  ascsortedMAP.put(35, "name35");
  ascsortedMAP.put(44, "name44");
  ascsortedMAP.put(7, "name7");
  ascsortedMAP.put(6, "name6"); 

  for(Map.Entry<Integer, String> mapData : ascsortedMAP.entrySet()) {
    System.out.println("Key : " +mapData.getKey()+ "Value : "+mapData.getValue());
    }

Sorting by descending ordering :

按降序排序:

  //Create the map and provide the comparator as a argument
  Map<Integer,String> dscsortedMAP = new TreeMap<Integer,String>(new Comparator<Integer>() 
     {
        @Override
        public int compare(Integer o1, Integer o2) {                
            return o2.compareTo(o1);
         }
     });
    dscsortedMAP.putAll(ascsortedMAP);

      for(Map.Entry<Integer, String> mapData : dscsortedMAP.entrySet()) {
        System.out.println("Key : " +mapData.getKey()+" Value : "+mapData.getValue());
        }

for further information about SortedMAP read http://examples.javacodegeeks.com/core-java/util/treemap/java-sorted-map-example/

有关SortedMAP的更多信息,请阅读http://examples.javacodegeeks.com/core-java/util/treemap/java-sorted-map-example/

#4


3  

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

TreeMap是SortedMap接口的一个实现,可以工作。

How do I use it ?

我该如何使用它?

Map<Float, MyObject> map = new TreeMap<Float, MyObject>();

#5


2  

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.

TreeMap按主要自然顺序排序。键应该实现Comparable或与Comparator兼容(如果您将一个实例传递给构造函数)。在你的情况下,Float已经实现了Comparable,所以你不需要做任何特别的事情。

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

您可以调用keySet以按升序检索所有键。