package java.util;import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;/*** 集合層次結構的根接口,一個集合表示一組對象,稱為元素* JDK不提供任何該接口的直接實現,JDK提供實現特定情況的子接口,Set,List** 所有通用Collection實現類通常需要通過一個間接實現Collection接口的子接口來實現,* 而且需要提供兩個標準的構造函數,沒有參數的構造函數(空參構造),創建一個空集合;* 以及包含Collection類型的單個參數的構造函數,使用與其參數相同的元素創建一個新集合。* @author Josh Bloch* @author Neal Gafter* @see Set* @see List* @see Map* @see SortedSet* @see SortedMap* @see HashSet* @see TreeSet* @see ArrayList* @see LinkedList* @see Vector* @see Collections* @see Arrays* @see AbstractCollection* @since 1.2*/
public interface Collection<E> extends Iterable<E> {// 查詢操作/*** 返回此集合中的元素數。如果這個集合 包含多于<tt> Integer.MAX_VALUE </ tt>的元素,返回 <tt> Integer.MAX_VALUE </ tt>。** @return 此集合中的元素數量*/int size();/*** 如果此集合不包含任何元素,則返回<tt> true </ tt>* @return <tt>true</tt> 如果此集合不包含任何元素*/boolean isEmpty();/**** 如果集合中包含至少一個指定對象,返回true* @param o 要測試其在此集合中的存在的元素* @return <tt>true</tt> 如果集合中包含該元素** @throws ClassCastException 如果指定元素的類型 與集合內元素類型不兼容* @throws NullPointerException 如果指定的元素為null且此 collection不允許null元素**/boolean contains(Object o);/*** 返回此集合中元素的迭代器。無法保證元素的返回順序*(除非此集合是某個提供保證的類的實例)。** @return an <tt>Iterator</tt> 覆蓋此集合中的元素*/Iterator<E> iterator();/*** 返回包含此集合中所有元素的數組。* 如果此集合對其迭代器返回的元素的順序做出任何保證,則此方法必須以相同的順序返回元素。** 返回的數組將是“安全的”,因為此集合不會保留對它的引用。* (換句話說,即使此集合由數組支持,此方法也必須分配新數組)。* 調用者因此可以自由修改返回的數組。** @return 包含此集合中所有元素的數組*/Object[] toArray();/*** 返回包含此集合中所有元素的數組;* 返回數組的運行時類型是指定數組的運行時類型。* 如果集合適合指定的數組,則返回元素到指定的數組中。* 否則,將使用指定數組的運行時類型和此集合的大小分配新數組。** 此集合適合指定的數組,如果有空余空間(即,數組的元素多于此集合),則緊跟集合結尾的數組中的元素將設置為<tt> null </ TT>。* (如果調用者知道此集合不包含任何<tt> null </ tt>元素,則此選項僅用于確定此集合的長度<i> </ i>。)** 如果此集合對其迭代器返回的元素的順序做出任何保證,則此方法必須以相同的順序返回元素。** @param <T> 包含集合的數組的運行時類型* @throws ArrayStoreException 如果指定數組的運行時類型不是此集合中每個元素的運行時類型的超類型* @throws NullPointerException 如果指定的數組為null*/<T> T[] toArray(T[] a);// 修改操作/*** 將元素e添加到集合中* 如果集合不允許重復元素,且集合中已經含有該元素,返回false* false-添加失敗*/boolean add(E e);/*** 從該 collection 中移除指定元素的單個實例,如果集合中存在指定元素返回true。*/boolean remove(Object o);// 批量操作/*** 如果此 collection 包含指定 collection 中的所有元素,則返回 true。*/boolean containsAll(Collection<?> c);/*** 將指定 collection 中的所有元素都添加到此 collection 中*/boolean addAll(Collection<? extends E> c);/*** 移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。*/boolean removeAll(Collection<?> c);/**** 刪除此集合中滿足給定布爾值函數的所有元素。在迭代期間或通過布爾值函數拋出的錯誤或運行時異常被中繼到調用者。* 默認使用迭代器進行刪除元素** @param filter 一個布爾值函數,它返回{@code true}表示要刪除的元素* @return {@code true} 如果刪除了任何元素* @since 1.8*/default boolean removeIf(Predicate<? super E> filter) {Objects.requireNonNull(filter);boolean removed = false;final Iterator<E> each = iterator();while (each.hasNext()) {if (filter.test(each.next())) {each.remove();removed = true;}}return removed;}/**** 僅保留此集合中包含在指定集合中的元素。換句話說,從此集合中刪除未包含在指定集合中的所有元素。*/boolean retainAll(Collection<?> c);/*** 移除此 collection 中的所有元素。*/void clear();// 比較和散列/*** 比較此 collection 與指定對象是否相等。通過覆蓋,實現list與list相等,set與set相等*/boolean equals(Object o);/*** 返回此 collection 的哈希碼值。*/int hashCode();/**** Spliterator是一個可分割迭代器(splitable iterator),可以和iterator順序遍歷迭代器一起看。jdk1.8發布后,對于并行處理的能力大大增強,Spliterator就是為了并行遍歷元素而設計的一個迭代器,jdk1.8中的集合框架中的數據結構都默認實現了spliterator** 在此集合中的元素上創建{@link Spliterator}。** 后續如果講到了{@link Spliterator}再進行補充下面的三個方法*** Implementations should document characteristic values reported by the* spliterator. Such characteristic values are not required to be reported* if the spliterator reports {@link Spliterator#SIZED} and this collection* contains no elements.** <p>The default implementation should be overridden by subclasses that* can return a more efficient spliterator. In order to* preserve expected laziness behavior for the {@link #stream()} and* {@link #parallelStream()}} methods, spliterators should either have the* characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be* <em><a href="Spliterator.html#binding">late-binding</a></em>.* If none of these is practical, the overriding class should describe the* spliterator's documented policy of binding and structural interference,* and should override the {@link #stream()} and {@link #parallelStream()}* methods to create streams using a {@code Supplier} of the spliterator,* as in:* <pre>{@code* Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics)* }</pre>* <p>These requirements ensure that streams produced by the* {@link #stream()} and {@link #parallelStream()} methods will reflect the* contents of the collection as of initiation of the terminal stream* operation.** @implSpec* The default implementation creates a* <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator* from the collections's {@code Iterator}. The spliterator inherits the* <em>fail-fast</em> properties of the collection's iterator.* <p>* The created {@code Spliterator} reports {@link Spliterator#SIZED}.** @implNote* The created {@code Spliterator} additionally reports* {@link Spliterator#SUBSIZED}.** <p>If a spliterator covers no elements then the reporting of additional* characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED},* does not aid clients to control, specialize or simplify computation.* However, this does enable shared use of an immutable and empty* spliterator instance (see {@link Spliterators#emptySpliterator()}) for* empty collections, and enables clients to determine if such a spliterator* covers no elements.** @return a {@code Spliterator} over the elements in this collection* @since 1.8*/@Overridedefault Spliterator<E> spliterator() {return Spliterators.spliterator(this, 0);}/*** Returns a sequential {@code Stream} with this collection as its source.** <p>This method should be overridden when the {@link #spliterator()}* method cannot return a spliterator that is {@code IMMUTABLE},* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}* for details.)** @implSpec* The default implementation creates a sequential {@code Stream} from the* collection's {@code Spliterator}.** @return a sequential {@code Stream} over the elements in this collection* @since 1.8*/default Stream<E> stream() {return StreamSupport.stream(spliterator(), false);}/*** Returns a possibly parallel {@code Stream} with this collection as its* source. It is allowable for this method to return a sequential stream.** <p>This method should be overridden when the {@link #spliterator()}* method cannot return a spliterator that is {@code IMMUTABLE},* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}* for details.)** @implSpec* The default implementation creates a parallel {@code Stream} from the* collection's {@code Spliterator}.** @return a possibly parallel {@code Stream} over the elements in this* collection* @since 1.8*/default Stream<E> parallelStream() {return StreamSupport.stream(spliterator(), true);}
}
?