NavigableMap接口源码重点
- NavigableMap接口继承自SortedMap接口,提供有序Map接口的扩展,提供了更为细致的导航方法,SortedMap源码可以看我这篇文章 SortedMap
- SortedMap接口继承自Map接口,所以NavigableMap接口也可以说是一种Map接口
- NavigableMap接口提供了四类方法分别是
- 根据给定键key查找集合Map中的键
- 根据给定键key查找集合Map中的键值对Entry
- 获取集合Map中的键最小或最大的键值对
- 有序Map集合处理方法(构造逆序集合或子集合)
NavigableMap接口方法
方法名 | 作用 |
---|---|
K lowerKey(K key) | 返回小于给定键key的最大键(最接近key,但小于key),如果没有此类键,则返回null |
K floorKey(K key) | 返回小于或等于给定key的最大键(最接近key,但小于等于key),如果没有此类键,则返回null |
K higherKey(K key) | 返回大于给定键key的最小键(最接近key,但大于key),如果没有此类键,则返回null |
K ceilingKey(K key) | 返回大于或等于给定键key的最小键(最接近key,但大于等于key),如果没有此类键,则返回null |
Map.Entry<K,V> lowerEntry(K key) | 返回小于给定键key的最大键关联的键值对Entry,如果没有此类键,则返回null |
Map.Entry<K,V> floorEntry(K key) | 返回小于或等于给定键key的最大键关联的键值对Entry,如果没有此类键,则返回null |
Map.Entry<K,V> higherEntry(K key) | 返回大于给定键key的最小键对应的键值对Entry,如果没有此类键,则返回null |
Map.Entry<K,V> ceilingEntry(K key) | 返回大于或等于给定键key的最小键对应的键值对Entry,如果没有此类键,则返回null |
Map.Entry<K,V> firstEntry() | 返回与此Map中最小键对应的键值对Entry,如果键值对为空,则返回null |
Map.Entry<K,V> lastEntry() | 返回与此Map中最大键对应的键值对Entry,如果键值对为空,则返回null |
Map.Entry<K,V> pollFirstEntry() | 移除并返回与此Map中最小键对应的键值对Entry,如果键值对为空,则返回null |
Map.Entry<K,V> pollLastEntry() | 移除并返回与此Map中最大键对应的键值对Entry,如果键值对为空,则返回null |
NavigableMap descendingMap() | 返回此NavigableMap的逆序排序的NavigableMap |
NavigableSet navigableKeySet() | 返回此Map中包含的键的NavigableSet集合,相当于返回包含key的Set集合,NavigableSet类似NavigableMap提供导航方法 |
NavigableSet descendingKeySet() | 返回此Map中包含的键的逆序NavigableSet集合 |
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) | 返回一个子NavigableMap,此子NavigableMap包含此NavigableMap键范围从fromKey到toKey的部分 |
NavigableMap<K,V> headMap(K toKey, boolean inclusive) | 返回一个子NavigableMap,此子NavigableMap包含此NavigableMap键范围小于(或等于,如果inclusive为true)toKey的部分 |
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) | 返回一个子NavigableMap,此子NavigableMap包含此NavigableMap键范围大于(或等于,如果 inclusive为true)fromKey的部分 |
SortedMap<K,V> subMap(K fromKey, K toKey) | 继承并重写父类SortedMap的方法,返回一个子SortedMap,和上面subMap方法作用类似 |
SortedMap<K,V> headMap(K toKey) | 继承并重写父类SortedMap的方法,返回一个子SortedMap,和上面headMap方法作用类似 |
SortedMap<K,V> tailMap(K fromKey) | 继承并重写父类SortedMap的方法,返回一个子SortedMap,和上面tailMap方法作用类似 |
NavigableMap接口源码
package java.util;
/**
* A {@link SortedMap} extended with navigation methods returning the
* closest matches for given search targets. Methods
* {@link #lowerEntry}, {@link #floorEntry}, {@link #ceilingEntry},
* and {@link #higherEntry} return {@code Map.Entry} objects
* associated with keys respectively less than, less than or equal,
* greater than or equal, and greater than a given key, returning
* {@code null} if there is no such key. Similarly, methods
* {@link #lowerKey}, {@link #floorKey}, {@link #ceilingKey}, and
* {@link #higherKey} return only the associated keys. All of these
* methods are designed for locating, not traversing entries.
* 一个SortedMap扩展了导航方法,返回给定搜索目标的最接近匹配项。
* 方法lowerEntry、floorEntry、ceilingEntry、和higherEntry返回Map.Entry对象,
* 这些对象分别与小于、小于或等于、大于或等于、大于给定键相关联,
* 如果没有这样的键,则返回null。类似地,方法lowerKey、
* floorKey、ceilingKey和higherKey只返回相关键。
* 所有这些方法都是为定位而设计的,而不是遍历条目。
*
*
*
*
*
*
*
* <p>A {@code NavigableMap} may be accessed and traversed in either
* ascending or descending key order. The {@link #descendingMap}
* method returns a view of the map with the senses of all relational
* and directional methods inverted. The performance of ascending
* operations and views is likely to be faster than that of descending
* ones. Methods
* {@link #subMap(Object, boolean, Object, boolean) subMap(K, boolean, K, boolean)},
* {@link #headMap(Object, boolean) headMap(K, boolean)}, and
* {@link #tailMap(Object, boolean) tailMap(K, boolean)}
* differ from the like-named {@code SortedMap} methods in accepting
* additional arguments describing whether lower and upper bounds are
* inclusive versus exclusive. Submaps of any {@code NavigableMap}
* must implement the {@code NavigableMap} interface.
* NavigableMap可以按键的升序或降序进行访问和遍历。descendingMap方法返回一个映射视图,
* 其中所有关系方法和方向方法的意义都颠倒了。
* 升序操作和视图的性能可能比降序操作和视图的性能更快。
* 方法subMap(Object, boolean, Object, boolean)、
* headMap(Object, boolean)和tailMap(Object, boolean)
* 不同于类似的SortedMap方法接受描述上下界是包含的还是排除的附加参数。
* 任何NavigableMap的子映射必须实现NavigableMap接口。
*
*
*
*
*
*
* <p>This interface additionally defines methods {@link #firstEntry},
* {@link #pollFirstEntry}, {@link #lastEntry}, and
* {@link #pollLastEntry} that return and/or remove the least and
* greatest mappings, if any exist, else returning {@code null}.
* 此接口还定义了方法firstEntry、pollFirstEntry、lastEntry和pollLastEntry,
* 这些方法返回和/或删除最小和最大的映射(如果存在),否则返回null
*
*
*
*
*
*
* <p>Implementations of entry-returning methods are expected to
* return {@code Map.Entry} pairs representing snapshots of mappings
* at the time they were produced, and thus generally do <em>not</em>
* support the optional {@code Entry.setValue} method. Note however
* that it is possible to change mappings in the associated map using
* method {@code put}.
* 条目返回方法的实现应该返回Map.entry对,这些对表示生成映射时的快照,
* 因此通常不支持可选的Entry.setValue方法。
* 但是请注意,可以使用方法put更改关联映射中的映射。
*
*
*
* <p>Methods
* {@link #subMap(Object, Object) subMap(K, K)},
* {@link #headMap(Object) headMap(K)}, and
* {@link #tailMap(Object) tailMap(K)}
* are specified to return {@code SortedMap} to allow existing
* implementations of {@code SortedMap} to be compatibly retrofitted to
* implement {@code NavigableMap}, but extensions and implementations
* of this interface are encouraged to override these methods to return
* {@code NavigableMap}. Similarly,
* {@link #keySet()} can be overridden to return {@link NavigableSet}.
* 方法subMap(Object, Object)、headMap(Object)和tailMap(Object)
* 被指定为返回SortedMap,以允许兼容地改装SortedMap的现有实现 NavigableMap,
* 但是鼓励该接口的扩展和实现重写这些方法以返回NavigableMap。类似地,
* 可以重写keySet()以返回NavigableSet
*
*
*
*
* <p>This interface is a member of the
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
* Java Collections Framework</a>.
*
* @author Doug Lea
* @author Josh Bloch
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @since 1.6
*/
public interface NavigableMap<K,V> extends SortedMap<K,V> {
/**
* Returns a key-value mapping associated with the greatest key
* strictly less than the given key, or {@code null} if there is
* no such key.
* 返回与严格小于给定键的最大键关联的键值映射,
* 如果没有这样的键,则返回null
*
* @param key the key
* @return an entry with the greatest key less than {@code key},
* or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
Map.Entry<K,V> lowerEntry(K key);
/**
* Returns the greatest key strictly less than the given key, or
* {@code null} if there is no such key.
* 返回严格小于给定键的最大键,如果没有此类键,则返回null
*
* @param key the key
* @return the greatest key less than {@code key},
* or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
K lowerKey(K key);
/**
* Returns a key-value mapping associated with the greatest key
* less than or equal to the given key, or {@code null} if there
* is no such key.
* 返回与小于或等于给定键的最大键关联的键值映射,
* 如果没有此类键,则返回null
*
* @param key the key
* @return an entry with the greatest key less than or equal to
* {@code key}, or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
Map.Entry<K,V> floorEntry(K key);
/**
* Returns the greatest key less than or equal to the given key,
* or {@code null} if there is no such key.
* 返回小于或等于给定键的最大键,如果没有此类键,则返回null
*
* @param key the key
* @return the greatest key less than or equal to {@code key},
* or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
K floorKey(K key);
/**
* Returns a key-value mapping associated with the least key
* greater than or equal to the given key, or {@code null} if
* there is no such key.
* 返回与大于或等于给定键的最小键相关联的键值映射,如果没有这样的键,
* 则返回null
*
* @param key the key
* @return an entry with the least key greater than or equal to
* {@code key}, or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
Map.Entry<K,V> ceilingEntry(K key);
/**
* Returns the least key greater than or equal to the given key,
* or {@code null} if there is no such key.
* 返回大于或等于给定键的最小键,如果没有此类键,则返回null
*
* @param key the key
* @return the least key greater than or equal to {@code key},
* or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
K ceilingKey(K key);
/**
* Returns a key-value mapping associated with the least key
* strictly greater than the given key, or {@code null} if there
* is no such key.
* 返回与严格大于给定键的最小键关联的键值映射,如果没有这样的键,
* 则返回null
*
* @param key the key
* @return an entry with the least key greater than {@code key},
* or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
Map.Entry<K,V> higherEntry(K key);
/**
* Returns the least key strictly greater than the given key, or
* {@code null} if there is no such key.
* 返回严格大于给定键的最小键,如果没有此类键,则返回null
*
* @param key the key
* @return the least key greater than {@code key},
* or {@code null} if there is no such key
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map does not permit null keys
*/
K higherKey(K key);
/**
* Returns a key-value mapping associated with the least
* key in this map, or {@code null} if the map is empty.
* 返回与此映射中最小键关联的键值映射,如果映射为空,
* 则返回null
*
* @return an entry with the least key,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> firstEntry();
/**
* Returns a key-value mapping associated with the greatest
* key in this map, or {@code null} if the map is empty.
* 返回与此映射中最大键关联的键值映射,如果映射为空,则返回null
*
* @return an entry with the greatest key,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> lastEntry();
/**
* Removes and returns a key-value mapping associated with
* the least key in this map, or {@code null} if the map is empty.
* 移除并返回与此映射中最小键关联的键值映射,如果映射为空,则返回null
*
* @return the removed first entry of this map,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> pollFirstEntry();
/**
* Removes and returns a key-value mapping associated with
* the greatest key in this map, or {@code null} if the map is empty.
* 移除并返回与此映射中最大键关联的键值映射,如果映射为空,则返回null
*
* @return the removed last entry of this map,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> pollLastEntry();
/**
* Returns a reverse order view of the mappings contained in this map.
* The descending map is backed by this map, so changes to the map are
* reflected in the descending map, and vice-versa. If either map is
* modified while an iteration over a collection view of either map
* is in progress (except through the iterator's own {@code remove}
* operation), the results of the iteration are undefined.
* 返回此映射中包含的映射的逆序视图。
* 降序贴图由该贴图支持,因此对贴图的更改将反映在降序贴图中,反之亦然。
* 如果在对任一映射的集合视图进行迭代时修改了任一映射
* (通过迭代器自己的remove操作除外),则迭代的结果是未定义的。
*
*
*
* <p>The returned map has an ordering equivalent to
* {@link Collections#reverseOrder(Comparator) Collections.reverseOrder}{@code (comparator())}.
* The expression {@code m.descendingMap().descendingMap()} returns a
* view of {@code m} essentially equivalent to {@code m}.
* 返回的映射的排序等价于Collections#reverseOrder(Comparator)
* comparator()。表达式m.descendingMap().descendingMap()返回一个 m的视图本质上等同于m
*
*
* @return a reverse order view of this map
*/
NavigableMap<K,V> descendingMap();
/**
* Returns a {@link NavigableSet} view of the keys contained in this map.
* The set's iterator returns the keys in ascending order.
* The set is backed by the map, so changes to the map are reflected in
* the set, and vice-versa. If the map is modified while an iteration
* over the set is in progress (except through the iterator's own {@code
* remove} operation), the results of the iteration are undefined. The
* set supports element removal, which removes the corresponding mapping
* from the map, via the {@code Iterator.remove}, {@code Set.remove},
* {@code removeAll}, {@code retainAll}, and {@code clear} operations.
* It does not support the {@code add} or {@code addAll} operations.
* 返回此映射中包含的键的NavigableSet视图。集合的迭代器按升序返回键。
* 集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。
* 如果在对集合进行迭代时修改映射(通过迭代器自己的remove操作除外),
* 则迭代的结果是未定义的。该集合支持元素移除,
* 即通过Iterator.remove、set.remove、removeAll、
* retainal和clear操作从映射中移除相应的映射。
* 它不支持add或addAll操作。
*
*
*
* @return a navigable set view of the keys in this map
*/
NavigableSet<K> navigableKeySet();
/**
* Returns a reverse order {@link NavigableSet} view of the keys contained in this map.
* The set's iterator returns the keys in descending order.
* The set is backed by the map, so changes to the map are reflected in
* the set, and vice-versa. If the map is modified while an iteration
* over the set is in progress (except through the iterator's own {@code
* remove} operation), the results of the iteration are undefined. The
* set supports element removal, which removes the corresponding mapping
* from the map, via the {@code Iterator.remove}, {@code Set.remove},
* {@code removeAll}, {@code retainAll}, and {@code clear} operations.
* It does not support the {@code add} or {@code addAll} operations.
* 返回此映射中包含的键的逆序NavigableSet视图。集合的迭代器按降序返回键。
* 集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。
* 如果在对集合进行迭代时修改映射(通过迭代器自己的remove操作除外),
* 则迭代的结果是未定义的。该集合支持元素移除,
* 即通过Iterator.remove、Set.remove、removeAll、retainAll和clear
* 操作从映射中移除相应的映射。它不支持add或addAll操作。
*
*
*
*
* @return a reverse order navigable set view of the keys in this map
*/
NavigableSet<K> descendingKeySet();
/**
* Returns a view of the portion of this map whose keys range from
* {@code fromKey} to {@code toKey}. If {@code fromKey} and
* {@code toKey} are equal, the returned map is empty unless
* {@code fromInclusive} and {@code toInclusive} are both true. The
* returned map is backed by this map, so changes in the returned map are
* reflected in this map, and vice-versa. The returned map supports all
* optional map operations that this map supports.
* 返回此映射中键范围从fromKey到toKey的部分的视图。
* 如果fromKey和toKey相等,则返回的映射为空,除非fromInclusive和toInclusive都为真。
* 返回的映射由该映射支持,因此返回映射中的更改将反映在该映射中,反之亦然。
* 返回的映射支持此映射支持的所有可选映射操作。
*
*
*
*
*
* <p>The returned map will throw an {@code IllegalArgumentException}
* on an attempt to insert a key outside of its range, or to construct a
* submap either of whose endpoints lie outside its range.
* 返回的映射将抛出IllegalArgumentException,以尝试在其范围之外插入键,
* 或构造端点位于其范围之外的子映射。
*
*
*
*
* @param fromKey low endpoint of the keys in the returned map
* @param fromInclusive {@code true} if the low endpoint
* is to be included in the returned view
* @param toKey high endpoint of the keys in the returned map
* @param toInclusive {@code true} if the high endpoint
* is to be included in the returned view
* @return a view of the portion of this map whose keys range from
* {@code fromKey} to {@code toKey}
* @throws ClassCastException if {@code fromKey} and {@code toKey}
* cannot be compared to one another using this map's comparator
* (or, if the map has no comparator, using natural ordering).
* Implementations may, but are not required to, throw this
* exception if {@code fromKey} or {@code toKey}
* cannot be compared to keys currently in the map.
* @throws NullPointerException if {@code fromKey} or {@code toKey}
* is null and this map does not permit null keys
* @throws IllegalArgumentException if {@code fromKey} is greater than
* {@code toKey}; or if this map itself has a restricted
* range, and {@code fromKey} or {@code toKey} lies
* outside the bounds of the range
*/
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive);
/**
* Returns a view of the portion of this map whose keys are less than (or
* equal to, if {@code inclusive} is true) {@code toKey}. The returned
* map is backed by this map, so changes in the returned map are reflected
* in this map, and vice-versa. The returned map supports all optional
* map operations that this map supports.
* 返回此映射中键小于(或等于,如果inclusive为true)toKey的部分的视图。
* 返回的映射由该映射支持,因此返回映射中的更改将反映在该映射中,反之亦然。
* 返回的映射支持此映射支持的所有可选映射操作。
*
*
*
* <p>The returned map will throw an {@code IllegalArgumentException}
* on an attempt to insert a key outside its range.
*
* @param toKey high endpoint of the keys in the returned map
* @param inclusive {@code true} if the high endpoint
* is to be included in the returned view
* @return a view of the portion of this map whose keys are less than
* (or equal to, if {@code inclusive} is true) {@code toKey}
* @throws ClassCastException if {@code toKey} is not compatible
* with this map's comparator (or, if the map has no comparator,
* if {@code toKey} does not implement {@link Comparable}).
* Implementations may, but are not required to, throw this
* exception if {@code toKey} cannot be compared to keys
* currently in the map.
* @throws NullPointerException if {@code toKey} is null
* and this map does not permit null keys
* @throws IllegalArgumentException if this map itself has a
* restricted range, and {@code toKey} lies outside the
* bounds of the range
*/
NavigableMap<K,V> headMap(K toKey, boolean inclusive);
/**
* Returns a view of the portion of this map whose keys are greater than (or
* equal to, if {@code inclusive} is true) {@code fromKey}. The returned
* map is backed by this map, so changes in the returned map are reflected
* in this map, and vice-versa. The returned map supports all optional
* map operations that this map supports.
* 返回此映射中键大于(或等于,如果 inclusive为true)fromKey的部分的视图。
* 返回的映射由该映射支持,因此返回映射中的更改将反映在该映射中,反之亦然。
* 返回的映射支持此映射支持的所有可选映射操作。
*
*
*
* <p>The returned map will throw an {@code IllegalArgumentException}
* on an attempt to insert a key outside its range.
*
* @param fromKey low endpoint of the keys in the returned map
* @param inclusive {@code true} if the low endpoint
* is to be included in the returned view
* @return a view of the portion of this map whose keys are greater than
* (or equal to, if {@code inclusive} is true) {@code fromKey}
* @throws ClassCastException if {@code fromKey} is not compatible
* with this map's comparator (or, if the map has no comparator,
* if {@code fromKey} does not implement {@link Comparable}).
* Implementations may, but are not required to, throw this
* exception if {@code fromKey} cannot be compared to keys
* currently in the map.
* @throws NullPointerException if {@code fromKey} is null
* and this map does not permit null keys
* @throws IllegalArgumentException if this map itself has a
* restricted range, and {@code fromKey} lies outside the
* bounds of the range
*/
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
/**
* {@inheritDoc}
*
* <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.
* 相当于subMap(fromKey, true, toKey, false)
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedMap<K,V> subMap(K fromKey, K toKey);
/**
* {@inheritDoc}
*
* <p>Equivalent to {@code headMap(toKey, false)}.
* 相当于headMap(toKey, false)
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedMap<K,V> headMap(K toKey);
/**
* {@inheritDoc}
*
* <p>Equivalent to {@code tailMap(fromKey, true)}.
* 相当于tailMap(fromKey, true)
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedMap<K,V> tailMap(K fromKey);
}