Java SortedMap示例
时间:2020-02-23 14:37:15  来源:igfitidea点击:
SortedMap接口扩展了映射,并确保所有条目都按升序排列(因此SortedMap)。
如果你想让它按降序排列,你需要覆盖SortedMap中的Compare方法,我们马上就要做。TreeMap实现SortedMap,它或者按键的自然顺序排序,或者按指定的顺序排序比较器。英寸不允许树映射空键和空值。
方法总结
比较器<?super K>comparator():返回用于对当前映射中的键排序的比较器,如果当前映射使用其键的自然顺序,则返回null。
设置<地图。入口<K,V>>entrySet():返回当前映射中包含的映射的集合视图。
K firstKey():返回当前映射中的第一个键(最低或者最高,取决于实现映射的方式(升序或者降序))。
SortedMap<K,V>headMap(K toKey):返回当前地图中键严格小于toKey的部分的视图。
Set<K>keySet():返回当前映射中包含的键的集合视图
K lastKey():返回当前映射中的最后一个(最高或者最低)键
SortedMap<K,V>subMap(K fromKey,K toKey):返回当前地图的端口视图,其leys仅从fromKey到toKey
从地图上返回的关键点大于当前地图的关键点
Collection<V>values():返回当前映射中包含的值的集合视图
有关这些方法的更多详细信息,请查看Oracle官方文档。
代码实现
import java.util.*;
public class SortedHashMapExample {
 public static void main(String args[]) {
    Map<Double, String> players = new TreeMap<Double, String>();
    //health, name
    players.put(new Double(100.00), "Hill");
    players.put(new Double(120.00), "John");
    players.put(new Double(150.00), "Sabrina");
    players.put(new Double(105.00), "Caitlyn");
    players.put(new Double(110.00), "Rachel");
    players.put(new Double(130.00), "Michael");
    players.put(new Double(140.00), "Mark");
    //get a set of the entries
    Set setOfEntries = players.entrySet();
    //get an iterator
    Iterator iterator = setOfEntries.iterator();
    while(iterator.hasNext()) {
        //create an entry of the map
       Map.Entry entry = (Map.Entry)iterator.next();
       System.out.println("Key: " + entry.getKey());
       System.out.println("Value: " + entry.getValue());
    }
 }
}
输出
Key: 100.0 Value: Hill Key: 105.0 Value: Caitlyn Key: 110.0 Value: Rachel Key: 120.0 Value: John Key: 130.0 Value: Michael Key: 140.0 Value: Mark Key: 150.0 Value: Sabrina
如我们所见,它自动按升序对它们进行分组。从100.00点开始到150.00点。我之所以把健康作为一个键,把名字作为一个值,只是为了告诉你它提升了它们。
但是如果我们想按降序排列呢?
降序实现
import java.util.*;
public class SortedHashMapExample {
 public static void main(String args[]) {
    Map<Double, String> players = new TreeMap<Double, String>(new Comparator<Double>() {
      @Override
      public int compare(Double x, Double y) {
        return y.compareTo(x);
      }
    });
    //name, health
    players.put(new Double(100.00), "Hill");
    players.put(new Double(120.00), "John");
    players.put(new Double(150.00), "Sabrina");
    players.put(new Double(105.00), "Caitlyn");
    players.put(new Double(110.00), "Rachel");
    players.put(new Double(130.00), "Michael");
    players.put(new Double(140.00), "Mark");
    //get a set of the entries
    Set setOfEntries = players.entrySet();
    //get an iterator
    Iterator iterator = setOfEntries.iterator();
    while(iterator.hasNext()) {
        //create an entry of the map
       Map.Entry entry = (Map.Entry)iterator.next();
       System.out.println("Key: " + entry.getKey());
       System.out.println("Value: " + entry.getValue());
    }
 }
}
输出
Key: 150.0 Value: Sabrina Key: 140.0 Value: Mark Key: 130.0 Value: Michael Key: 120.0 Value: John Key: 110.0 Value: Rachel Key: 105.0 Value: Caitlyn Key: 100.0 Value: Hill
我们所做的只是重写compare方法,而不是x=>y(升序),而是改为y=>x(降序)。

