基础篇-Set-TreeSet

前言:

  • 树:一种逻辑的存储数据的结构,这种结构就像是一颗颠倒的树,包括根结点,节点
Set
TreeSet
HsahSet
  • 链表:增删速度快,数组:查询速度快,树:二者性能折中
  • 树的度:当前节点的子节点数量
  • 树的层:从根节点到最底叶子结点
  • 树的高度:层-1

Set

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

不包含重复元素的集合。更正式地说,集合不包含一对元素 e1 和 e2,使得 e1.equals(e2),并且最多包含一个空元素。正如其名称所暗示的那样,该接口对数学集合抽象进行建模。

The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.)

除了从 Collection 接口继承的那些之外,Set 接口对所有构造函数的契约以及 add、equals 和 hashCode 方法的契约进行了额外的规定。为方便起见,此处还包括其他继承方法的声明。 (这些声明随附的规范已针对 Set 接口量身定制,但不包含任何其他规定。)

The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements (as defined above).

对构造函数的额外规定是,所有构造函数都必须创建一个不包含重复元素的集合(如上所定义),这并不奇怪。

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

注意:如果将可变对象用作集合元素,则必须非常小心。如果对象的值以影响等于比较的方式更改,而对象是集合中的元素,则不会指定集合的​​行为。此禁止的一个特殊情况是不允许集合将自身包含为元素。

Some set implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the set may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as “optional” in the specification for this interface.

一些集合实现对它们可能包含的元素有限制。例如,有些实现禁止空元素,有些实现对其元素的类型有限制。尝试添加不合格的元素会引发未经检查的异常,通常为 NullPointerException 或 ClassCastException。尝试查询不合格元素的存在可能会引发异常,或者可能只是返回 false;一些实现会表现出前一种行为,而另一些会表现出后者。更一般地,尝试对不合格元素执行操作,其完成不会导致将不合格元素插入到集合中,可能会引发异常,或者可能会成功,具体取决于实现的选择。在此接口的规范中,此类异常被标记为“可选”。

This interface is a member of the Java Collections Framework.

AbstractSet

This class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface.

此类提供 Set 接口的骨架实现,以最大限度地减少实现此接口所需的工作。

The process of implementing a set by extending this class is identical to that of implementing a Collection by extending AbstractCollection, except that all of the methods and constructors in subclasses of this class must obey the additional constraints imposed by the Set interface (for instance, the add method must not permit addition of multiple instances of an object to a set).

通过扩展此类实现集合的过程与通过扩展 AbstractCollection 实现集合的过程相同,除了此类的子类中的所有方法和构造函数都必须遵守 Set 接口强加的附加约束(例如, add 方法不得允许将一个对象的多个实例添加到一个集合中)。

Note that this class does not override any of the implementations from the AbstractCollection class. It merely adds implementations for equals and hashCode.

请注意,此类不会覆盖 AbstractCollection 类的任何实现。它只是添加了 equals 和 hashCode 的实现。

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

基于 TreeMap 的 NavigableSet 实现。元素使用它们的自然顺序进行排序,或者通过在集合创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。

TreeSet

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

此实现为基本操作(添加、删除和包含)提供有保证的 log(n) 时间成本。

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

请注意,如果要正确实现 Set 接口,则集合维护的排序(无论是否提供显式比较器)必须与 equals 一致。 (请参阅 Comparable 或 Comparator 以获取与 equals 一致的精确定义。)这是因为 Set 接口是根据 equals 操作定义的,但是 TreeSet 实例使用它的 compareTo(或 compare)方法执行所有元素比较,所以两个被此方法视为相等的元素,从集合的角度来看,是相等的。集合的行为是明确定义的,即使它的顺序与 equals 不一致;它只是不遵守 Set 接口的一般约定。

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 must 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 Collections.synchronizedSortedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(…));

请注意,此实现不是同步的。如果多个线程同时访问一个树集,并且至少有一个线程修改了该集,则必须在外部进行同步。这通常是通过同步一些自然封装集合的对象来实现的。如果不存在此类对象,则应使用 Collections.synchronizedSortedSet 方法“包装”该集合。这最好在创建时完成,以防止对集合的意外不同步访问:
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(…));

The iterators returned by this class’s iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

此类的迭代器方法返回的迭代器是快速失败的:如果在迭代器创建后的任何时间修改了集合,除了通过迭代器自己的 remove 方法以外的任何方式,迭代器都会抛出 ConcurrentModificationException。因此,面对并发修改,迭代器快速而干净地失败,而不是在未来不确定的时间冒着任意、非确定性行为的风险。

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

请注意,无法保证迭代器的快速失败行为,因为一般而言,在存在非同步并发修改的情况下不可能做出任何硬保证。快速失败的迭代器会尽最大努力抛出 ConcurrentModificationException。因此,编写一个依赖此异常来确保其正确性的程序是错误的:迭代器的快速失败行为应该仅用于检测错误。

This class is a member of the Java Collections Framework.

此类是 Java 集合框架的成员。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值