public class CopyOnWriteArraySet 
   extends AbstractSet<E> implements Serializable
| java.lang.Object | |||
| ↳ | java.util.AbstractCollection<E> | ||
| ↳ | java.util.AbstractSet<E> | ||
| ↳ | java.util.concurrent.CopyOnWriteArraySet<E> | ||
A Set ,其所有操作均使用内部CopyOnWriteArrayList 。 因此,它具有相同的基本属性:
add, set, remove, etc.) are expensive since they usually entail copying the entire underlying array. remove operation. 样例用法。 以下代码草图使用写时复制集来维护一组Handler对象,这些对象在状态更新时执行某些操作。
 class Handler { void handle(); ... }
 class X {
   private final CopyOnWriteArraySet<Handler> handlers
     = new CopyOnWriteArraySet<>();
   public void addHandler(Handler h) { handlers.add(h); }
   private long internalState;
   private synchronized void changeState() { internalState = ...; }
   public void update() {
     changeState();
     for (Handler handler : handlers)
       handler.handle();
   }
 } 
     
    也可以看看:
Public constructors |  
      |
|---|---|
  CopyOnWriteArraySet()  创建一个空集。  |  
      |
  CopyOnWriteArraySet(Collection<? extends E> c)  创建一个包含指定集合的所有元素的集合。  |  
      |
Public methods |  
      |
|---|---|
 boolean  |  
         add(E e)  如果指定的元素不存在,则将其添加到此集合中。  |  
      
 boolean  |  
         addAll(Collection<? extends E> c)  如果它们不存在,则将指定集合中的所有元素添加到此集合中。  |  
      
 void  |  
         clear()  删除此组中的所有元素。  |  
      
 boolean  |  
         contains(Object o)  如果此集合包含指定的元素,则返回   |  
      
 boolean  |  
         containsAll(Collection<?> c)  如果此集合包含指定集合的所有元素,则返回   |  
      
 boolean  |  
         equals(Object o)  将指定的对象与此集合进行相等比较。  |  
      
 void  |  
         forEach(Consumer<? super E> action)   |  
      
 boolean  |  
         isEmpty()  如果此集合不包含元素,则返回   |  
      
 Iterator<E>  |  
         iterator()  按照添加这些元素的顺序,返回此集合中包含的元素的迭代器。  |  
      
 boolean  |  
         remove(Object o)  如果存在,则从该集合中删除指定的元素。  |  
      
 boolean  |  
         removeAll(Collection<?> c)  从该集合中移除指定集合中包含的所有元素。  |  
      
 boolean  |  
         removeIf(Predicate<? super E> filter)   |  
      
 boolean  |  
         retainAll(Collection<?> c)  仅保留指定集合中包含的此集合中的元素。  |  
      
 int  |  
         size()  返回此集合中的元素数量。  |  
      
 Spliterator<E>  |  
         spliterator()  按照添加这些元素的顺序,在此集合中的元素上返回一个   |  
      
 <T> T[]  |  
         toArray(T[] a)  返回包含此集合中所有元素的数组; 返回数组的运行时类型是指定数组的运行时类型。  |  
      
 Object[]  |  
         toArray()  返回包含此集合中所有元素的数组。  |  
      
Inherited methods |  
      |
|---|---|
  java.util.AbstractSet  
         |  
      |
  java.util.AbstractCollection  
         |  
      |
  java.lang.Object  
         |  
      |
  java.util.Set  
         |  
      |
  java.util.Collection  
         |  
      |
  java.lang.Iterable  
         |  
      |
CopyOnWriteArraySet (Collection<? extends E> c)
创建一个包含指定集合的所有元素的集合。
| Parameters | |
|---|---|
c |  
         Collection: the collection of elements to initially contain |  
       
| Throws | |
|---|---|
NullPointerException |  
        if the specified collection is null | 
boolean add (E e)
如果指定的元素不存在,则将其添加到此集合中。 更正式地,将指定的元素e这一套如果集合不包含元素e2这样Objects.equals(e, e2) 。 如果这个集合已经包含这个元素,那么这个调用离开这个集合并且返回false 。
| Parameters | |
|---|---|
e |  
         E: element to be added to this set |  
       
| Returns | |
|---|---|
boolean |  
        true if this set did not already contain the specified element  |  
       
boolean addAll (Collection<? extends E> c)
如果它们不存在,则将指定集合中的所有元素添加到此集合中。 如果指定的集合也是一个集合,则addAll操作会有效地修改此集合,使其值为两个集合的并集。 如果在操作过程中修改了指定的集合,则此操作的行为未定义。
| Parameters | |
|---|---|
c |  
         Collection: collection containing elements to be added to this set |  
       
| Returns | |
|---|---|
boolean |  
        true if this set changed as a result of the call |  
       
| Throws | |
|---|---|
NullPointerException |  
        if the specified collection is null | 
也可以看看:
boolean contains (Object o)
如果此集合包含指定的元素,则返回true 。 更正式地说,返回true当且仅当这个集合包含一个元素e例如Objects.equals(o, e) 。
| Parameters | |
|---|---|
o |  
         Object: element whose presence in this set is to be tested |  
       
| Returns | |
|---|---|
boolean |  
        true if this set contains the specified element  |  
       
boolean containsAll (Collection<?> c)
如果此集合包含指定集合的所有元素,则返回true 。 如果指定的集合也是集合,则此方法返回true如果它是此集合的子集。
| Parameters | |
|---|---|
c |  
         Collection: collection to be checked for containment in this set |  
       
| Returns | |
|---|---|
boolean |  
        true if this set contains all of the elements of the specified collection |  
       
| Throws | |
|---|---|
NullPointerException |  
        if the specified collection is null | 
也可以看看:
boolean equals (Object o)
将指定的对象与此集合进行相等比较。 返回true如果指定对象是相同的对象,因为这对象,或者如果它是一个也Set并通过返回的元件iterator在指定的一组中的相同通过在该组的迭代器返回的元素。 更正式地说,如果两个迭代器返回相同数量的元素并且对于迭代器在指定集上返回的每个元素e1返回相同的元素,那么迭代器通过该集合返回元素e2 ,使得Objects.equals(e1, e2) 。
| Parameters | |
|---|---|
o |  
         Object: object to be compared for equality with this set |  
       
| Returns | |
|---|---|
boolean |  
        true if the specified object is equal to this set  |  
       
boolean isEmpty ()
如果此集合不包含元素,则返回 true 。
| Returns | |
|---|---|
boolean |  
        true if this set contains no elements  |  
       
Iterator<E> iterator ()
按照添加这些元素的顺序,返回此集合中包含的元素的迭代器。
返回的迭代器在构造迭代器时提供集合状态的快照。 遍历迭代器时不需要同步。 该迭代器不支持remove方法。
| Returns | |
|---|---|
Iterator<E> |  
        an iterator over the elements in this set | 
boolean remove (Object o)
如果存在,则从该集合中删除指定的元素。 更正式地说,删除一个元素e ,使得Objects.equals(o, e) ,如果这个集合包含这样一个元素。 如果此集合包含该元素(或等同于该集合因呼叫而改变),则返回true 。 (一旦调用返回,此集合将不包含该元素。)
| Parameters | |
|---|---|
o |  
         Object: object to be removed from this set, if present |  
       
| Returns | |
|---|---|
boolean |  
        true if this set contained the specified element  |  
       
boolean removeAll (Collection<?> c)
从该集合中移除指定集合中包含的所有元素。 如果指定的集合也是集合,则此操作会有效地修改此集合,使其值为两个集合的不对称集合差异 。
| Parameters | |
|---|---|
c |  
         Collection: collection containing elements to be removed from this set |  
       
| Returns | |
|---|---|
boolean |  
        true if this set changed as a result of the call |  
       
| Throws | |
|---|---|
ClassCastException |  
        if the class of an element of this set is incompatible with the specified collection (optional) | 
NullPointerException |  
        if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null | 
也可以看看:
boolean removeIf (Predicate<? super E> filter)
| Parameters | |
|---|---|
filter |  
         Predicate 
          |  
       
| Returns | |
|---|---|
boolean |  
        |
boolean retainAll (Collection<?> c)
仅保留指定集合中包含的此集合中的元素。 换句话说,从该集合中删除所有未包含在指定集合中的元素。 如果指定的集合也是集合,则此操作将有效地修改此集合,使其值为两个集合的交集 。
| Parameters | |
|---|---|
c |  
         Collection: collection containing elements to be retained in this set |  
       
| Returns | |
|---|---|
boolean |  
        true if this set changed as a result of the call |  
       
| Throws | |
|---|---|
ClassCastException |  
        if the class of an element of this set is incompatible with the specified collection (optional) | 
NullPointerException |  
        if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null | 
也可以看看:
Spliterator<E> spliterator ()
按照添加这些元素的顺序,在此集合中的元素上返回一个 Spliterator 。
该 Spliterator报告 IMMUTABLE , DISTINCT , SIZED ,并 SUBSIZED 。
分割器提供分割器构建时集合状态的快照。 在分割器上操作时不需要同步。
| Returns | |
|---|---|
Spliterator<E> |  
        a Spliterator over the elements in this set |  
       
T[] toArray (T[] a)
返回包含此集合中所有元素的数组; 返回数组的运行时类型是指定数组的运行时类型。 如果该集合适合指定的数组,它将返回其中。 否则,将使用指定数组的运行时类型和该组的大小分配一个新数组。
如果这个集合符合指定的数组并且有空余空间(即,数组的元素多于这个集合),那么紧跟在集合结束之后的数组中的元素被设置为null 。 ( 只有当调用者知道这个集合不包含任何空元素时,这在确定这个集合的长度时很有用。)
如果这个集合保证它的迭代器返回它的元素的顺序,这个方法必须以相同的顺序返回元素。
与toArray()方法一样,此方法充当基于数组和基于集合的API之间的桥梁。 此外,该方法允许精确控制输出数组的运行时类型,并且在某些情况下可以用于节省分配成本。
假设x是一个已知只包含字符串的集合。 以下代码可用于将该集转储到新分配的String数组中:
 String[] y = x.toArray(new String[0]); Note that 
     toArray(new Object[0]) is identical in function to 
     toArray(). 
      
     | Parameters | |
|---|---|
a |  
         T: the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. |  
       
| Returns | |
|---|---|
T[] |  
        an array containing all the elements in this set | 
| Throws | |
|---|---|
ArrayStoreException |  
        if the runtime type of the specified array is not a supertype of the runtime type of every element in this set | 
NullPointerException |  
        if the specified array is null | 
Object[] toArray ()
返回包含此集合中所有元素的数组。 如果这个集合保证它的迭代器返回它的元素的顺序,这个方法必须以相同的顺序返回元素。
返回的数组将是“安全的”,因为这个集合没有引用它。 (换句话说,即使这个集合由数组支持,这个方法也必须分配一个新数组)。 调用者可以自由修改返回的数组。
此方法充当基于数组和基于集合的API之间的桥梁。
| Returns | |
|---|---|
Object[] |  
        an array containing all the elements in this set |