java容器 接口NavigableMap源码分析

目录

简介

lowerXXX,floorXXX,ceilingXXX,higherXXX,总共8个

firstEntry,lastEntry,pollFirstEntry,pollLastEntry

descendingMap,navigableKeySet,descendingKeySet

subMap,tailMap,headMap,每个2个


简介

/**
 * 一个SortedMap扩展了navigation方法,为给定的搜索目标返回最接近的匹配。
 * 方法lowerEntry, floorEntry, ceilingEntry, 和 higherEntry返回
 * Map.Entry对象,分别与小于、小于或等于、大于或等于、大于给定键关联的条目对象,如果没有这样的键,则返回null。
 * 类似地,方法lowerKey、floorKey、ceilingKey和higherKey只返回关联的键。所有这些方法都是为定位而设计的,而不是遍历条目。
 *
 * 

可按升序或降序键顺序访问和遍历NavigableMap。 * descendingMap方法返回映射的一个视图,该视图具有所有反向的关系和反转方向的方法。 * ascending操作和视图的性能可能比descending操作和视图的性能更快。 * 方法subMap、headMap和tailMap与类似名称的SortedMap方法不同,它们接受额外的参数来描述下界和上界是包含的还是排他的。 * 任何NavigableMap的子映射都必须实现NavigableMap接口。 * *

该接口还定义了firstEntry、pollFirstEntry、lastEntry和pollLastEntry方法, * 这些方法返回和/或删除最小和最大的映射(如果存在的话),否则返回null。 * *

返回entry方法的实现应该返回Map.Entry对,表示生成映射时的快照,因此通常不支持可选的setValue方法。 * 但是请注意,可以使用put方法更改关联映射中的映射。 * *

方法subMap(K, K)、headMap(K)和tailMap(K)被指定为返回SortedMap, * 以允许对SortedMap的现有实现进行兼容的改进以实现NavigableMap,但是这个接口的扩展和实现被鼓励重写这些方法以返回NavigableMap。 * 类似地,可以覆盖keySet()来返回NavigableSet。 * * * @author Doug Lea * @author Josh Bloch * @param the type of keys maintained by this map * @param the type of mapped values * @since 1.6 */ public interface NavigableMap extends SortedMap

java容器 接口NavigableMap源码分析_第1张图片

lowerXXX,floorXXX,ceilingXXX,higherXXX,总共8个

    /**
     * 返回严格小于给定键的与最大键关联的键-值映射,如果没有这样的键,则返回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 lowerEntry(K 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);

    /**
     * 返回与小于或等于给定键的最大键相关联的键-值映射,如果没有这样的键,则返回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 floorEntry(K key);

    /**
     * Returns the greatest key less than or equal to the given key,
     * or {@code null} if there is no such key.
     *
     * @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);

    /**
     * 返回与大于或等于给定键的最小键关联的键-值映射,如果没有这样的键,则返回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 ceilingEntry(K 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);

    /**
     * 返回与严格大于给定键的最小键关联的键-值映射,如果没有这样的键,则返回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 higherEntry(K 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);

firstEntry,lastEntry,pollFirstEntry,pollLastEntry

    /**
     * 返回与此映射中最小键关联的键-值映射,如果映射为空则返回null。
     *
     * @return an entry with the least key,
     *         or {@code null} if this map is empty
     */
    Map.Entry firstEntry();

    /**
     * 返回与此映射中最大键关联的键-值映射,如果映射为空则返回null。
     *
     * @return an entry with the greatest key,
     *         or {@code null} if this map is empty
     */
    Map.Entry lastEntry();

    /**
     * 删除并返回与此映射中最小键关联的键-值映射,如果映射为空则返回null。
     *
     * @return the removed first entry of this map,
     *         or {@code null} if this map is empty
     */
    Map.Entry pollFirstEntry();

    /**
     * 删除并返回与此映射中最大键关联的键-值映射,如果映射为空则返回null。
     *
     * @return the removed last entry of this map,
     *         or {@code null} if this map is empty
     */
    Map.Entry pollLastEntry();

descendingMap,navigableKeySet,descendingKeySet

    /**
     * 返回此映射中包含的映射的逆序视图。descending映射由该映射支持,因此对映射的更改反映在descending映射中,反之亦然。
     * 如果在对任何一个map的集合视图进行迭代时(除了通过迭代器自己的删除操作之外),其中一个map被修改,那么迭代的结果是未定义的。
     *
     * 

返回的映射的顺序相当于Collections.reverseOrder(comparator())。 * 表达式{@code m.descendingMap().descendingMap()}返回一个m的视图,它本质上等价于m。 * * @return a reverse order view of this map */ NavigableMap descendingMap(); /** * 返回包含在此映射中的键的NavigableSet视图。 * 集合的迭代器按升序返回键。集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。 * 如果在对集合进行迭代时修改了映射(除了通过迭代器自己的删除操作),则迭代的结果是未定义的。 * 集合支持元素删除,它通过Iterator.remove, Set.remove, removeAll, retainAll, and clear 操作。 * 它不支持add或addAll操作。 * * @return a navigable set view of the keys in this map */ NavigableSet navigableKeySet(); /** * 返回包含在此映射中的键的逆序NavigableSet视图。 * 集合的迭代器按降序返回键。集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。 * 如果在对集合进行迭代时修改了映射(除了通过迭代器自己的删除操作),则迭代的结果是未定义的。 * 集合支持元素删除,它通过Iterator.remove, Set.remove, removeAll, retainAll, and clear 操作。 * 它不支持add或addAll操作。 * * @return a reverse order navigable set view of the keys in this map */ NavigableSet descendingKeySet();

subMap,tailMap,headMap,每个2个

    /**
     * 返回该映射中键值范围从fromKey到toKey的部分的视图。
     * 如果fromKey和toKey相等,则返回的映射为空,除非fromInclusive和toInclusive都为true。
     * 返回的映射得到此映射的支持,因此返回映射中的更改将反映在此映射中,反之亦然。
     * 返回的映射支持此映射支持的所有可选映射操作。
     *
     * 

返回的映射将抛出一个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 subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive); /** * 返回映射中键值小于(或等于,如果inclusive为true)toKey部分的视图。 * 返回的映射得到此映射的支持,因此返回映射中的更改将反映在此映射中,反之亦然。 * 返回的映射支持此映射支持的所有可选映射操作。 * *

返回的映射将抛出一个IllegalArgumentException, * 用于试图将一个键插入到它的范围之外,或者构造一个其端点位于它的范围之外的子映射。 * * @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 headMap(K toKey, boolean inclusive); /** * 返回映射中键值大于(或等于,如果inclusive为true)fromKey的部分的视图。 * 返回的映射得到此映射的支持,因此返回映射中的更改将反映在此映射中,反之亦然。 * 返回的映射支持此映射支持的所有可选映射操作。 * *

返回的映射将抛出一个IllegalArgumentException, * 用于试图将一个键插入到它的范围之外,或者构造一个其端点位于它的范围之外的子映射。 * * @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 tailMap(K fromKey, boolean inclusive); /** * {@inheritDoc} * *

等于 {@code subMap(fromKey, true, toKey, false)}. * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ SortedMap subMap(K fromKey, K toKey); /** * {@inheritDoc} * *

等于 {@code headMap(toKey, false)}. * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ SortedMap headMap(K toKey); /** * {@inheritDoc} * *

等于 {@code tailMap(fromKey, true)}. * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ SortedMap tailMap(K fromKey);

你可能感兴趣的:(java容器,源码分析)