The SortedMap Interface

SortedMap is a Map that maintains its entries in ascending order, sorted according to the keys‘ natural ordering, or according to a Comparator provided at the time of the SortedMapcreation. Natural ordering and Comparators are discussed in the Object Ordering section. The SortedMap interface provides operations for normal Map operations and for the following:

  • Range view — performs arbitrary range operations on the sorted map
  • Endpoints — returns the first or the last key in the sorted map
  • Comparator access — returns the Comparator, if any, used to sort the map

The following interface is the Map analog of SortedSet.

public interface SortedMap<K, V> extends Map<K, V>{
    Comparator<? super K> comparator();
    SortedMap<K, V> subMap(K fromKey, K toKey);
    SortedMap<K, V> headMap(K toKey);
    SortedMap<K, V> tailMap(K fromKey);
    K firstKey();
    K lastKey();
}

Map Operations

The operations SortedMap inherits from Map behave identically on sorted maps and normal maps with two exceptions:

  • The Iterator returned by the iterator operation on any of the sorted map‘s Collection views traverse the collections in order.
  • The arrays returned by the Collection views‘ toArray operations contain the keys, values, or entries in order.

Although it isn‘t guaranteed by the interface, the toString method of the Collection views in all the Java platform‘s SortedMap implementations returns a string containing all the elements of the view, in order.

Standard Constructors

By convention, all general-purpose Map implementations provide a standard conversion constructor that takes a MapSortedMap implementations are no exception. In TreeMap, this constructor creates an instance that orders its entries according to their keys‘ natural ordering. This was probably a mistake. It would have been better to check dynamically to see whether the specified Map instance was a SortedMap and, if so, to sort the new map according to the same criterion (comparator or natural ordering). Because TreeMap took the approach it did, it also provides a constructor that takes a SortedMap and returns a new TreeMap containing the same mappings as the given SortedMap, sorted according to the same criterion. Note that it is the compile-time type of the argument, not its runtime type, that determines whether the SortedMap constructor is invoked in preference to the ordinary map constructor.

SortedMap implementations also provide, by convention, a constructor that takes a Comparator and returns an empty map sorted according to the specified Comparator. If null is passed to this constructor, it returns a Map that sorts its mappings according to their keys‘ natural ordering.

Comparison to SortedSet

Because this interface is a precise Map analog of SortedSet, all the idioms and code examples in The SortedSet Interface section apply to SortedMap with only trivial modifications.

时间: 2024-10-12 17:24:02

The SortedMap Interface的相关文章

Java - The SortedMap Interface

The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key order Several methods throw a NoSuchElementException when no items are in the invoking map. A ClassCastException is thrown when an object is incompatible

Java Sorted Map Example

In this example we shall show you how to make use of Java Sorted Map. A SortedMap is a Map that sort its entries in ascending order according to the keys’ natural ordering, or according to a Comparator provided at the time of the SortedMap creation.

SortedMap接口源码解析

TreeMap的父接口 package java.util; public interface SortedMap<K,V> extends Map<K,V> { Comparator<? super K> comparator(); SortedMap<K,V> subMap(K fromKey, K toKey); SortedMap<K,V> headMap(K toKey); SortedMap<K,V> tailMap(K

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

Interface接口

一.接口及作用 接口是一个非常重要的概念,理解这个,先举一个实现生活中的实例. 现在,电脑主板,有很多插槽,可用来插CPU.内存等.当CPU等元件需要更新换代升级时,只要将CPU单独更换就可以了,而不需要更换主板.其实,主板上的这些暴露在外的插槽,就可以理解为接口. 接口就是对外暴露的规则,只要符合规则的CPU.内存,不论品牌.型号.规格,都可以安插使用. 接口是程序的功能扩展.有了插槽,也就提高了主板的功能扩展性,比如内存不够用了,我们就可以换成内容最大的内存条,或再加新的内存条. 接口降低了

一个Interface 继承多个Interface 的总结

我们知道在Java中的继承都是单继承的,就是说一个父类可以被多个子类继承但是一个子类只能有一个父类.但是一个接口可以被不同实现类去实现,这就是我们说的Java中的多态的概念.下面我们再来说一下接口的多继承的概念,就是说一个接口是可以继承多个接口的. 下面是我们公司自己开发的ORM框架,就用到了接口的多继承的概念. @MybatisRepository public interface BaseQueryDao<T> extends IBaseQueryDao<T>, Dynamic

【翻译】Android Interface Definition Language (AIDL)

参考地址:https://developer.android.com/guide/components/aidl.html Android Interface Definition Language (AIDL) AIDL (Android Interface Definition Language) is similar to other IDLs you might have worked with. It allows you to define the programming inter

JAVA之旅(七)——final关键字 , 抽象类abstract,模板方法模式,接口interface,implements,特点,扩展

JAVA之旅(七)--final关键字 , 抽象类abstract,模板方法模式,接口interface,implements,特点,扩展 OK,我们继续学习JAVA,美滋滋的 一.final 我们来聊聊final这个关键字 final可以修饰类,方法和变量 final修饰的类不可以被继承 final修饰的方法不可以被覆盖 final修饰的变量是一个常量,只能被赋值一次 内部类只能访问被final修饰的局部变量 final,故名思意,就是最终的意思,由以上的五种特性,不过final的出现,也是有

重构第9天:提取接口(Extract Interface)

理解:提取接口的意思是,多于一个类共同使用某个类中的方法或属性,那么我们可以把这些方法和属性提出来,作为一个单独的接口.这样的好处是解除代码间的依赖,降低耦合性. 详解: 先看重构前的代码: 1 public class ClassRegistration 2 { 3 public void Create() 4 { 5 // create registration code 6 } 7 8 public void Transfer() 9 { 10 // class transfer code