Java SortedMap接口总结 有序Map要实现的接口规范

SortedMap接口源码重点

  1. SortedMap接口继承自Map接口,自然具有Map接口定义的其他方法,Map源码可以看我这篇文章 Map
  2. 实现SortedMap接口表明该Map的键是根据排序规则有序的,SortedMap接口方法的 comparator()方法可以返回排序Map的排序规则,返回的Comparator是一个比较器,里面有一个虚方法compare(T o1, T o2) ,默认意思是如果compare(T o1, T o2) 返回值大于0,表示 o1 - o2 > 0,Comparator源码可以看我这篇文章 Comparator
  3. SortedMap接口定义的方法,可以随意获取排序Map的子Map,子Map和原Map共享一个空间的,可能都是键值对数组Entry[],所以修改子Map会影响原Map

SortedMap接口方法

方法名作用
Comparator comparator()返回用于对此Map中的键进行排序的比较器
SortedMap<K,V> subMap(K fromKey, K toKey)返回此Map的子Map,子Map键的范围从 fromKey(包含)到 toKey(除外)
SortedMap<K,V> headMap(K toKey)返回此Map中键小于toKey的部分键值对组成的子Map
SortedMap<K,V> tailMap(K fromKey)返回此Map中键大于或等于fromKey的部分键值对组成的子Map
K firstKey()返回此Map中当前的第一个(最小)键
K lastKey()返回此Map中当前的最后一个(最大)键
Set keySet()返回此Map中包含的键的Set集合,Set集合的迭代器会按升序返回键
Collection values()返回此Map中包含的值的Collection集合,Map集合的迭代器按相应键的升序返回值(也是按照键的升序)
Set<Map.Entry> entrySet()返回此Map中包含的键值对Entry的Set集合,Set集合的迭代器以升序键顺序返回键值对Entry

SortedMap接口源码

package java.util;

/**
 * A {@link Map} that further provides a <em>total ordering</em> on its keys.
 * The map is ordered according to the {@linkplain Comparable natural
 * ordering} of its keys, or by a {@link Comparator} typically
 * provided at sorted map creation time.  This order is reflected when
 * iterating over the sorted map's collection views (returned by the
 * {@code entrySet}, {@code keySet} and {@code values} methods).
 * Several additional operations are provided to take advantage of the
 * ordering.  (This interface is the map analogue of {@link SortedSet}.)
 * 一种Map,它进一步提供其键的总顺序。映射根据其键的Comparable排序,
 * 或者由通常在排序映射创建时提供的Comparator排序。
 * 迭代排序映射的集合视图(由entrySet、keySet和values方法返回)时,
 * 会反映此顺序。提供了几个附加操作以利用订购。
 * (此接口是SortedSet的映射模拟。)
 * 
 * 
 * 
 * 
 *
 * <p>All keys inserted into a sorted map must implement the {@code Comparable}
 * interface (or be accepted by the specified comparator).  Furthermore, all
 * such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} (or
 * {@code comparator.compare(k1, k2)}) must not throw a
 * {@code ClassCastException} for any keys {@code k1} and {@code k2} in
 * the sorted map.  Attempts to violate this restriction will cause the
 * offending method or constructor invocation to throw a
 * {@code ClassCastException}.
 * 插入到排序映射中的所有键都必须实现Comparable接口(或被指定的比较器接受)。
 * 此外,所有这些键必须是相互可比较的:k1.compareTo(k2)
 * (或comparator.compare(k1, k2) 不能为排序映射中的任何键k1和k2抛出ClassCastException
 * 违反此限制的尝试将导致有问题的方法或构造函数调用抛出ClassCastException
 * 
 * 
 * 
 *
 * <p>Note that the ordering maintained by a sorted map (whether or not an
 * explicit comparator is provided) must be <em>consistent with equals</em> if
 * the sorted map is to correctly implement the {@code Map} interface.  (See
 * the {@code Comparable} interface or {@code Comparator} interface for a
 * precise definition of <em>consistent with equals</em>.)  This is so because
 * the {@code Map} interface is defined in terms of the {@code equals}
 * operation, but a sorted map performs all key comparisons using its
 * {@code compareTo} (or {@code compare}) method, so two keys that are
 * deemed equal by this method are, from the standpoint of the sorted map,
 * equal.  The behavior of a tree map <em>is</em> well-defined even if its
 * ordering is inconsistent with equals; it just fails to obey the general
 * contract of the {@code Map} interface.
 * 请注意,如果排序映射要正确实现Map接口,则排序映射(无论是否提供显式比较器)维护的顺序必须与equals一致。(
 * 请参见Comparable接口或Comparator接口以获得与equals一致的精确定义。)
 * 这是因为Map接口是根据equals操作定义的,
 * 但排序映射使用其compareTo(或compare)方法执行所有键比较,
 * 因此,从排序映射的角度来看,用这种方法认为相等的两个键是相等的。
 * 树映射的行为是定义良好的,即使其顺序与equals不一致;
 * 它只是没有遵守Map接口的总约定。
 * 
 * 
 * 
 * 
 * 
 *
 * <p>All general-purpose sorted map implementation classes should provide four
 * "standard" constructors. It is not possible to enforce this recommendation
 * though as required constructors cannot be specified by interfaces. The
 * expected "standard" constructors for all sorted map implementations are:
 * 所有通用的排序映射实现类都应该提供四个“标准”构造函数。
 * 虽然接口不能指定as所需的构造函数,但不可能强制执行此建议。
 * 所有排序映射实现的预期“标准”构造函数为:
 * 
 * 
 * <ol>
 *   <li>A void (no arguments) constructor, which creates an empty sorted map
 *   sorted according to the natural ordering of its keys.</li>
 *   一个void(无参数)构造函数,它创建一个根据键的自然顺序排序的空排序映射。
 * 
 * 
 *   <li>A constructor with a single argument of type {@code Comparator}, which
 *   creates an empty sorted map sorted according to the specified comparator.</li>
 *   具有Comparator类型的单个参数的构造函数,它创建一个根据指定的比较器排序的空排序映射。
 * 
 * 
 *   <li>A constructor with a single argument of type {@code Map}, which creates
 *   a new map with the same key-value mappings as its argument, sorted
 *   according to the keys' natural ordering.</li>
 *   具有Map类型的单个参数的构造函数,该构造函数创建一个新的映射,
 *   该映射具有与其参数相同的键值映射,并根据键的自然顺序进行排序。
 * 
 * 
 *   <li>A constructor with a single argument of type {@code SortedMap}, which
 *   creates a new sorted map with the same key-value mappings and the same
 *   ordering as the input sorted map.</li>
 *   具有SortedMap类型的单个参数的构造函数,
 *   它创建一个新的排序映射,该映射具有与输入排序映射相同的键值映射和顺序。 
 *
 * </ol>
 *
 * <p><strong>Note</strong>: several methods return submaps with restricted key
 * ranges. Such ranges are <em>half-open</em>, that is, they include their low
 * endpoint but not their high endpoint (where applicable).  If you need a
 * <em>closed range</em> (which includes both endpoints), and the key type
 * allows for calculation of the successor of a given key, merely request
 * the subrange from {@code lowEndpoint} to
 * {@code successor(highEndpoint)}.  For example, suppose that {@code m}
 * is a map whose keys are strings.  The following idiom obtains a view
 * containing all of the key-value mappings in {@code m} whose keys are
 * between {@code low} and {@code high}, inclusive:
 * 注意:有几种方法返回具有受限key的子映射范围。
 * 这些范围是半开放的,也就是说,它们包括其低端,但不包括其高端(如适用)。
 * 如果您需要一个闭合范围(包括两个端点),并且键类型允许计算给定键的后续项,
 * 只需请求从lowEndpoin到 successor(highEndpoint)的子范围。
 * 例如,假设m是一个键为字符串的映射。下面的习惯用法获得一个视图,
 * 其中包含m中的所有键值映射,其键值介于low和high之间,包括:
 * 
 * 
 * 
 * 
 * <pre>
 *   SortedMap<String, V> sub = m.subMap(low, high);
 * </pre>
 *
 * A similar technique can be used to generate an <em>open range</em>
 * (which contains neither endpoint).  The following idiom obtains a
 * view containing all of the key-value mappings in {@code m} whose keys
 * are between {@code low} and {@code high}, exclusive:
 * 类似的技术可用于生成开放范围(其中既不包含端点也不包含端点)。
 * 以下习惯用法获得一个视图,其中包含m中的所有键值映射,
 * 其键值位于low和high之间,互斥:
 * 
 * 
 * <pre>
 *   SortedMap<String, V> sub = m.subMap(low, high);
 * </pre>
 *
 * <p>This interface is a member of the
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
 * Java Collections Framework</a>.
 *
 * @param <K> the type of keys maintained by this map
 * @param <V> the type of mapped values
 *
 * @author  Josh Bloch
 * @see Map
 * @see TreeMap
 * @see SortedSet
 * @see Comparator
 * @see Comparable
 * @see Collection
 * @see ClassCastException
 * @since 1.2
 */

public interface SortedMap<K,V> extends Map<K,V> {
    /**
     * Returns the comparator used to order the keys in this map, or
     * {@code null} if this map uses the {@linkplain Comparable
     * natural ordering} of its keys.
     * 返回用于对此映射中的键进行排序的比较器,
     * 如果此映射使用其键的Comparable 自然排序,则返回null
     *
     * @return the comparator used to order the keys in this map,
     *         or {@code null} if this map uses the natural ordering
     *         of its keys
     *         用于对该映射中的键进行排序的比较器,如果该映射使用其键的自然排序,则为null
     */
    Comparator<? super K> comparator();

    /**
     * Returns a view of the portion of this map whose keys range from
     * {@code fromKey}, inclusive, to {@code toKey}, exclusive.  (If
     * {@code fromKey} and {@code toKey} are equal, the returned map
     * is empty.)  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相等,则返回的映射为空。)返回的映射受此映射支持,
     * 因此返回映射中的更改将反映在此映射中,反之亦然。
     * 返回的映射支持此映射支持的所有可选映射操作。
     *
     * <p>The returned map will throw an {@code IllegalArgumentException}
     * on an attempt to insert a key outside its range.
     * 返回的映射将在尝试在其范围外插入键时抛出 IllegalArgumentException
     *
     * @param fromKey low endpoint (inclusive) of the keys in the returned map 返回的映射中键的低端(包括)
     * @param toKey high endpoint (exclusive) of the keys in the returned map 返回映射中键的高端(除外)
     * @return a view of the portion of this map whose keys range from
     *         {@code fromKey}, inclusive, to {@code toKey}, exclusive
     *         此映射部分的视图,其键的范围从fromKey(包含)到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.
     *         如果fromKey和toKey不能使用此映射的比较器相互比较(或者,如果映射没有比较器,则使用自然排序)。
     *         如果fromKey或toKey无法与映射中当前的键进行比较,则实现可能(但不要求)引发此异常。
     * 
     * 
     * @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
     */
    SortedMap<K,V> subMap(K fromKey, K toKey);

    /**
     * Returns a view of the portion of this map whose keys are
     * strictly less than {@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.
     * 返回此映射中键严格小于toKey的部分的视图。
     * 返回的映射由该映射支持,因此返回映射中的更改将反映在该映射中,反之亦然。
     * 返回的映射支持此映射支持的所有可选映射操作。
     *
     * <p>The returned map will throw an {@code IllegalArgumentException}
     * on an attempt to insert a key outside its range.
     *
     * @param toKey high endpoint (exclusive) of the keys in the returned map 返回映射中键的高端(除外)
     * @return a view of the portion of this map whose keys are strictly
     *         less than {@code toKey} 此映射中键严格小于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
     */
    SortedMap<K,V> headMap(K toKey);

    /**
     * Returns a view of the portion of this map whose keys are
     * greater than or equal to {@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.
     * 返回此映射中键大于或等于fromKey的部分的视图。
     * 返回的映射由该映射支持,因此返回映射中的更改将反映在该映射中,
     * 反之亦然。返回的映射支持此映射支持的所有可选映射操作。
     * 
     *
     * <p>The returned map will throw an {@code IllegalArgumentException}
     * on an attempt to insert a key outside its range.
     *
     * @param fromKey low endpoint (inclusive) of the keys in the returned map
     * @return a view of the portion of this map whose keys are greater
     *         than or equal to {@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
     */
    SortedMap<K,V> tailMap(K fromKey);

    /**
     * Returns the first (lowest) key currently in this map.
     * 返回此映射中当前的第一个(最低)键。
     *
     * @return the first (lowest) key currently in this map
     * @throws NoSuchElementException if this map is empty
     */
    K firstKey();

    /**
     * Returns the last (highest) key currently in this map.
     * 返回此映射中当前的最后一个(最高)键。
     *
     * @return the last (highest) key currently in this map
     * @throws NoSuchElementException if this map is empty
     */
    K lastKey();

    /**
     * Returns a {@link Set} 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.
     * 返回此映射中包含的键的Set视图。集合的迭代器按升序返回键。
     * 集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。
     * 如果在对集合进行迭代时修改映射(通过迭代器自己的remove操作除外),
     * 则迭代的结果是未定义的。该集合支持元素移除,即通过Iterator.remove、
     * Set.remove、removeAll、retainAll和clear操作从映射中移除相应的映射。
     * 它不支持add或addAll操作。
     *
     * @return a set view of the keys contained in this map, sorted in
     *         ascending order
     */
    Set<K> keySet();

    /**
     * Returns a {@link Collection} view of the values contained in this map.
     * The collection's iterator returns the values in ascending order
     * of the corresponding keys.
     * The collection is backed by the map, so changes to the map are
     * reflected in the collection, and vice-versa.  If the map is
     * modified while an iteration over the collection is in progress
     * (except through the iterator's own {@code remove} operation),
     * the results of the iteration are undefined.  The collection
     * supports element removal, which removes the corresponding
     * mapping from the map, via the {@code Iterator.remove},
     * {@code Collection.remove}, {@code removeAll},
     * {@code retainAll} and {@code clear} operations.  It does not
     * support the {@code add} or {@code addAll} operations.
     * 返回此映射中包含的值的Collection视图。集合的迭代器按相应键的升序返回值。
     * 集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。
     * 如果在对集合进行迭代时修改映射(通过迭代器自己的remove操作除外),
     * 则迭代的结果是未定义的。该集合支持元素移除,即通过Iterator.remove、
     * Set.remove、removeAll、retainAll和clear操作从映射中移除相应的映射。
     * 它不支持add或addAll操作。
     * 
     *
     * @return a collection view of the values contained in this map,
     *         sorted in ascending key order
     */
    Collection<V> values();

    /**
     * Returns a {@link Set} view of the mappings contained in this map.
     * The set's iterator returns the entries in ascending key 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, or through the
     * {@code setValue} operation on a map entry returned by the
     * iterator) 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.
     * 返回此映射中包含的映射的Set视图。集合的迭代器以升序键顺序返回条目。
     * 集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。
     * 如果在对集合进行迭代时修改映射(通过迭代器自己的remove操作除外),
     * 则迭代的结果是未定义的。该集合支持元素移除,即通过Iterator.remove、
     * Set.remove、removeAll、retainAll和clear操作从映射中移除相应的映射。
     * 它不支持add或addAll操作。
     * 
     * @return a set view of the mappings contained in this map,
     *         sorted in ascending key order
     */
    Set<Map.Entry<K, V>> entrySet();
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lolxxs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值