首页 > 其他分享 >Set---SortedSet-NavigableSet-TreeSet

Set---SortedSet-NavigableSet-TreeSet

时间:2023-11-08 19:24:00浏览次数:38  
标签:code SortedSet Set --- set NavigableSet null

SortedSet

概述

A {@link Set} that further provides a <i>total ordering</i> on its elements.
The elements are ordered using their {@linkplain Comparable natural ordering}, or by a {@link Comparator} typically provided at sorted set creation time.
The set's iterator will traverse the set in ascending element order.
Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of {@link SortedMap}.)

SortedSet支持元素排序;

SortedSet排序 使用元素的Comparable自然排序 或 创建时的Comparator;

Comparator的iterator将升序遍历;

 

All elements inserted into a sorted set must implement the <tt>Comparable</tt> interface (or be accepted by the specified comparator).
Furthermore, all such elements must be <i>mutually comparable</i>: <tt>e1.compareTo(e2)</tt> (or <tt>comparator.compare(e1, e2)</tt>) must not throw a <tt>ClassCastException</tt> for any elements <tt>e1</tt> and <tt>e2</tt> in the sorted set.
Attempts to violate this restriction will cause the offending method or constructor invocation to throw a <tt>ClassCastException</tt>.

SortedSet的所有元素 必须实现Comparable接口(或创建时指定comparator);

SortedSet的所有元素必须是可比较的;

 

Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be <i>consistent with equals</i> if the sorted set is to correctly implement the <tt>Set</tt> interface.
This is so because the <tt>Set</tt> interface is defined in terms of the <tt>equals</tt> operation, but a sorted set performs all element comparisons using its <tt>compareTo</tt> (or <tt>compare</tt>) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal.

实现Set接口的SortedSet的排序必须与equals一致;

因为Set使用equals比较,SortedSet使用Compare/CompareTo比较;

 

public interface SortedSet<E> extends Set<E> {
}

  

NavigableSet

概述

A {@link SortedSet} extended with navigation methods reporting closest matches for given search targets.
Methods {@code lower}, {@code floor}, {@code ceiling}, and {@code higher} return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning {@code null} if there is no such element.
A {@code NavigableSet} may be accessed and traversed in either ascending or descending order.

NavigableSet扩展了SortedSet,提供最接近查询目标的内容;

lower、floor、ceiling、higher 返回 <、<=、>=、>给定元素的内容;

NavigableSet可以 升序/降序遍历

The {@code descendingSet} method returns a view of the set 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.

descendingSet方法返回 降序的view;

升序的操作 性能优于 降序;

This interface additionally defines methods {@code pollFirst} and {@code pollLast} that return and remove the lowest and highest element, if one exists, else returning {@code null}.
Methods {@code subSet}, {@code headSet}, and {@code tailSet} differ from the like-named {@code SortedSet} methods in accepting additional arguments describing whether lower and upper bounds are inclusive versus exclusive.
Subsets of any {@code NavigableSet} must implement the {@code NavigableSet} interface.

pollFirst、pollLast 返回/移除 最低、最高的元素;

 

The return values of navigation methods may be ambiguous in implementations that permit {@code null} elements.
However, even in this case the result can be disambiguated by checking {@code contains(null)}.
To avoid such issues, implementations of this interface are encouraged to <em>not</em> permit insertion of {@code null} elements.

由于允许null,方法的返回值可能不明确;

鼓励实现NavigableSet接口的class不允许null;

 

public interface NavigableSet<E> extends SortedSet<E> {
}

  

TreeSet

概述

A {@link NavigableSet} implementation based on a {@link TreeMap}.
The elements are ordered using their {@linkplain Comparable natural ordering}, or by a {@link Comparator} provided at set creation time, depending on which constructor is used.

TreeSet是基于TreeMap的NavigableSet实现;

排序使用元素的Comparable自然排序 或 创建时提供的Comparator;

 

This implementation provides guaranteed log(n) time cost for the basic operations ({@code add}, {@code remove} and {@code contains}).

add、remove、contains话费 log(n) 的时间复杂度;

 

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be <i>consistent with equals</i> if it is to correctly implement the {@code Set} interface.  

实现Set接口的排序 必须与equals一致;

This is so because the {@code Set} interface is defined in terms of the {@code equals} operation, but a {@code TreeSet} instance performs all element comparisons using its {@code compareTo} (or {@code compare}) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. 

因为Set使用equals比较,TreeSet使用compareTo/compare比较;

 

Note that this implementation is not synchronized.
If multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it <i>must</i> be synchronized externally.
This is typically accomplished by synchronizing on some object that naturally encapsulates the set.
If no such object exists, the set should be "wrapped" using the {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet} method.

TreeSet是线程非同步的;

如果多个线程并发修改TreeSet结构,必须在外部进行同步;

可以使用Collections.synchronizedSortedSet;

 

The iterators returned by this class's {@code iterator} method are <i>fail-fast</i>: if the set is modified at any time after the iterator is created, in any way except through the iterator's own {@code remove} method, the iterator will throw a {@link ConcurrentModificationException}.

TreeSet的iterator是fail-fast:如果在iterator时进行remove(非iterator的remove)操作,将会抛出ConcurrentModificationException;

 

链路

add(E e)

// java.util.TreeSet.add
    public boolean add(E e) {
        return m.put(e, PRESENT)==null;
    }

    // java.util.TreeMap.put
    public V put(K key, V value) {
        TreeMap.Entry<K,V> t = root;
        if (t == null) {
            compare(key, key); // type (and possibly null) check

            root = new TreeMap.Entry<>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }
        int cmp;
        TreeMap.Entry<K,V> parent;
        // split comparator and comparable paths
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            do {
                parent = t;
                cmp = cpr.compare(key, t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        else {
            if (key == null)
                throw new NullPointerException();
            @SuppressWarnings("unchecked")
            Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        TreeMap.Entry<K,V> e = new TreeMap.Entry<>(key, value, parent);
        if (cmp < 0)
            parent.left = e;
        else
            parent.right = e;
        fixAfterInsertion(e);
        size++;
        modCount++;
        return null;
    }

  

 

标签:code,SortedSet,Set,---,set,NavigableSet,null
From: https://www.cnblogs.com/anpeiyong/p/17818103.html

相关文章

  • GPT-4 (All Tools):打造未来的开发工具
    随着GPT-4的推出,开发者们迎来了前所未有的插件功能,这些功能将极大地丰富他们的工作方式。"AllTools"作为GPT-4的一个重要组成部分,它提供了一系列强大的内置工具,可以帮助开发者更有效地编码、搜索内容、以及创建原型。Python代码执行工具首先,GPT-4内置了一个Python代码执行器。......
  • 基于hive旅游数据的分析与应用-计算机毕业设计源码+LW文档
    摘 要随着计算机技术发展,计算机系统的应用已延伸到社会的各个领域,大量基于网络的广泛应用给生活带来了十分的便利。所以把旅游数据管理与现在网络相结合,利用计算机搭建旅游数据的分析与应用系统,实现旅游数据的信息化。则对于进一步提高旅游数据管理发展,丰富旅游数据管理经验能起......
  • C++笔记 -- 使用STL的function实现回调机制(回调函数)
    1.使用普通函数示例一 代码:#include<iostream>#include<functional>//定义一个回调函数类型usingCallback=std::function<void(int)>;//定义一个函数,用于演示回调函数的使用voidperformOperation(intdata,Callbackcallback){//执......
  • setTimeout 是 DOM 提供的函数,不是JavaScript的全局函数
    JavaScript中包含以下7个全局函数,用于完成一些常用的功能(以后的章节中可能会用到):escape()、unescape()、eval()、isFinite()、isNaN()、parseFloat()、parseInt()函数描述decodeURI()解码某个编码的URI。decodeURIComponent()解码一个编码的URI组件。......
  • 智慧物业平台-计算机毕业设计源码+LW文档
    摘 要如今社会上各行各业,都喜欢用自己行业的专属软件工作,互联网发展到这个时候,人们已经发现离不开了互联网。新技术的产生,往往能解决一些老技术的弊端问题。因为传统智慧物业平台信息管理难度大,容错率低,管理人员处理数据费工费时,所以专门为解决这个难题开发了一个智慧物业平台管......
  • 分布式任务调度(03)--中心化设计
    把调度和任务执行,隔离成两个部分:调度中心只需要负责任务调度属性,触发调度命令执行器执行器接收调度命令,去执行具体的业务逻辑两者都可以进行横向扩容。1MQ调度中心依赖Quartz集群模式,当任务调度时,发送消息到RabbitMQ。业务应用收到任务消息后,消费任务信息。充分利......
  • springboot“共享书角”图书借还管理系统-计算机毕业设计源码+LW文档
    摘 要随着社会的发展,图书借还的管理形势越来越严峻。越来越多的借阅者利用互联网获得信息,但图书借还信息量大。为了方便借阅者更好的获得本图书借还信息,因此,设计一种安全高效的“共享书角”图书借还管理系统极为重要。为设计一个安全便捷,并且使借阅者更好获取本图书借还信息,本......
  • 无涯教程-批处理 - Remove函数
    字符串替换功能还可用于从另一个字符串中删除子字符串。Remove-示例@echooffsetstr=Batchscriptsiseasy.Itisreallyeasy.echo%str%setstr=%str:is=%echo%str%关于上述程序,需要注意的关键是,使用:'stringtoberemoved'=command从字符串中删除了"is"一词......
  • vue3 使用elementUI饿了么el-table组件 动态循环自定义表头列数据
     在vue3上使用el-table组件自定义循环表头列;<el-table:data="list"v-loading="loading"border>      <!--@selection-change="handleSelectionChange"-->      <!--<el-table-columntype="selection"wi......
  • OpenCV透视变换-不对原图裁剪
    前言:最近在做透视变换,初次尝试是使用Pillow的transform方法,但是该方法在变换以后,由于需要提前设置变换后的图像尺寸,所以导致变换后的图像超出尺寸的部分会发生裁剪问题,这个现象不太适合我的目的,我的目的是对png图进行透视变换,然后贴在一张图上。 后来用了opencv中的方法,这个方......