JAVA集合源码攻坚战(16)—— NavigableSet

前言

NavigableSet是对SortedSet的一个增强实现,能够进行数据的匹配。具体我们看分析代码。

正文

java.util 
Interface NavigableSet<E>

参数类型 
E - 由此集合维护的元素的类型 
All Superinterfaces: 
Collection <E>, Iterable <E>, Set <E>, SortedSet <E> 
所有已知实现类: 
ConcurrentSkipListSet , TreeSet 

NavigableSet继承了SortedSet,是对SortedSet的一个扩展,支持寻找最佳匹配元素。NavigableSet支持顺序遍历,也支持逆序遍历。
顺序操作的性能可能比逆序的更快一点。
该接口还定义了一些其他方法。

源码分析

查找操作

    /**
     * 返回严格小于给定元素的元素中最大的那个元素,如果没有比指定元素小的,则返回null。
     * Returns the greatest element in this set strictly less than the
     * given element, or {@code null} if there is no such element.
     *
     * @param e the value to match
     * @return the greatest element less than {@code e},
     *         or {@code null} if there is no such element
     * @throws ClassCastException if the specified element cannot be
     *         compared with the elements currently in the set
     * @throws NullPointerException if the specified element is null
     *         and this set does not permit null elements
     */
    E lower(E e);

    /**
     * 返回小于等于指定元素的所有元素中的最大的那个元素。如果没有比指定元素
     * 小的,则返回null。
     * Returns the greatest element in this set less than or equal to
     * the given element, or {@code null} if there is no such element.
     *
     * @param e the value to match
     * @return the greatest element less than or equal to {@code e},
     *         or {@code null} if there is no such element
     * @throws ClassCastException if the specified element cannot be
     *         compared with the elements currently in the set
     * @throws NullPointerException if the specified element is null
     *         and this set does not permit null elements
     */
    E floor(E e);

    /**
     * 返回大于等于指定元素的所有元素中那个最小的元素,如果没有大于等于指定元素的,
     * 则返回null。
     * Returns the least element in this set greater than or equal to
     * the given element, or {@code null} if there is no such element.
     *
     * @param e the value to match
     * @return the least element greater than or equal to {@code e},
     *         or {@code null} if there is no such element
     * @throws ClassCastException if the specified element cannot be
     *         compared with the elements currently in the set
     * @throws NullPointerException if the specified element is null
     *         and this set does not permit null elements
     */
    E ceiling(E e);

    /**
     * 返回严格大于指定元素的所有元素中的最小的那个元素,如果没有,则返回null。
     * Returns the least element in this set strictly greater than the
     * given element, or {@code null} if there is no such element.
     *
     * @param e the value to match
     * @return the least element greater than {@code e},
     *         or {@code null} if there is no such element
     * @throws ClassCastException if the specified element cannot be
     *         compared with the elements currently in the set
     * @throws NullPointerException if the specified element is null
     *         and this set does not permit null elements
     */
    E higher(E e);

查找并移除操作


        /**
     * 检索并移除第一个元素,即最小的那个元素。
     * 为什么是最小的按个,因为SortedSet中规定了,sortedSet集合都是按元素的递增排序的。
     * Retrieves and removes the first (lowest) element,
     * or returns {@code null} if this set is empty.
     *
     * @return the first element, or {@code null} if this set is empty
     */
    E pollFirst();

    /**
     * 检索并移除最后一个元素,即最大的元素。
     * Retrieves and removes the last (highest) element,
     * or returns {@code null} if this set is empty.
     *
     * @return the last element, or {@code null} if this set is empty
     */
    E pollLast();

迭代器

    /**返回当前set的一个翻转顺序的set集合。
     * 递减的set集合依赖于当前set,任何一个的改变都会影响另一个。如果在迭代过程中,
     * 没有使用迭代器自身的remove方法而修改了set,则迭代器的结果是未知的。
     * Returns a reverse order view of the elements contained in this set.
     * The descending set is backed by this set, so changes to the set are
     * reflected in the descending set, and vice-versa.  If either set is
     * modified while an iteration over either set is in progress (except
     * through the iterator's own {@code remove} operation), the results of
     * the iteration are undefined.
     *
     * <p>The returned set has an ordering equivalent to
     * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.
     * The expression {@code s.descendingSet().descendingSet()} returns a
     * view of {@code s} essentially equivalent to {@code s}.
     *
     * @return a reverse order view of this set
     */
    NavigableSet<E> descendingSet();

    /**
     * 返回一个递减的迭代器。与descendingSet().iterator()的效果相同。
     * Returns an iterator over the elements in this set, in descending order.
     * Equivalent in effect to {@code descendingSet().iterator()}.
     *
     * @return an iterator over the elements in this set, in descending order
     */
    Iterator<E> descendingIterator();

获得子集操作

1、返回当前

NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                           E toElement,   boolean toInclusive);

2、subSet的一个变种,只指定结束位置,从头开始。
NavigableSet<E> headSet(E toElement, boolean inclusive);

3、subSet的变种,只指定起始位置,直到结尾。

NavigableSet<E> tailSet(E fromElement, boolean inclusive);

4、三个返回类型为SortedSet的子集操作

/**
     * {@inheritDoc}
     *
     * <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}.
     *
     * @throws ClassCastException       {@inheritDoc}
     * @throws NullPointerException     {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     */
    SortedSet<E> subSet(E fromElement, E toElement);

    /**
     * {@inheritDoc}
     *
     * <p>Equivalent to {@code headSet(toElement, false)}.
     *
     * @throws ClassCastException       {@inheritDoc}
     * @throws NullPointerException     {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     */
    SortedSet<E> headSet(E toElement);

    /**
     * {@inheritDoc}
     *
     * <p>Equivalent to {@code tailSet(fromElement, true)}.
     *
     * @throws ClassCastException       {@inheritDoc}
     * @throws NullPointerException     {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     */
    SortedSet<E> tailSet(E fromElement);

对于这三个返回类型为SortedSet的方法,官方源码中特别做了说明:

Methods
{@link #subSet(Object, Object) subSet(E, E)},
{@link #headSet(Object) headSet(E)}, and
{@link #tailSet(Object) tailSet(E)}
are specified to return {@code SortedSet} to allow existing
implementations of {@code SortedSet} to be compatibly retrofitted to
implement {@code NavigableSet}, but extensions and implementations
of this interface are encouraged to override these methods to return
{@code NavigableSet}.

意思就是
方法:
{@link #subSet(Object, Object) subSet(E, E)},
{@link #headSet(Object) headSet(E)}, and
{@link #tailSet(Object) tailSet(E)}
在这里被指定为返回SortedSet,来使得当前的SortedSet的实现和NavigableSet相兼容,但鼓励重写该接口的这些方法,从而来返回NavigableSet。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值