Collection源码阅读
生活随笔
收集整理的這篇文章主要介紹了
Collection源码阅读
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package java.util;import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;/*** 集合層次結(jié)構(gòu)的根接口,一個(gè)集合表示一組對(duì)象,稱為元素* JDK不提供任何該接口的直接實(shí)現(xiàn),JDK提供實(shí)現(xiàn)特定情況的子接口,Set,List** 所有通用Collection實(shí)現(xiàn)類通常需要通過一個(gè)間接實(shí)現(xiàn)Collection接口的子接口來實(shí)現(xiàn),* 而且需要提供兩個(gè)標(biāo)準(zhǔn)的構(gòu)造函數(shù),沒有參數(shù)的構(gòu)造函數(shù)(空參構(gòu)造),創(chuàng)建一個(gè)空集合;* 以及包含Collection類型的單個(gè)參數(shù)的構(gòu)造函數(shù),使用與其參數(shù)相同的元素創(chuàng)建一個(gè)新集合。* @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> {// 查詢操作/*** 返回此集合中的元素?cái)?shù)。如果這個(gè)集合 包含多于<tt> Integer.MAX_VALUE </ tt>的元素,返回 <tt> Integer.MAX_VALUE </ tt>。** @return 此集合中的元素?cái)?shù)量*/int size();/*** 如果此集合不包含任何元素,則返回<tt> true </ tt>* @return <tt>true</tt> 如果此集合不包含任何元素*/boolean isEmpty();/**** 如果集合中包含至少一個(gè)指定對(duì)象,返回true* @param o 要測(cè)試其在此集合中的存在的元素* @return <tt>true</tt> 如果集合中包含該元素** @throws ClassCastException 如果指定元素的類型 與集合內(nèi)元素類型不兼容* @throws NullPointerException 如果指定的元素為null且此 collection不允許null元素**/boolean contains(Object o);/*** 返回此集合中元素的迭代器。無法保證元素的返回順序*(除非此集合是某個(gè)提供保證的類的實(shí)例)。** @return an <tt>Iterator</tt> 覆蓋此集合中的元素*/Iterator<E> iterator();/*** 返回包含此集合中所有元素的數(shù)組。* 如果此集合對(duì)其迭代器返回的元素的順序做出任何保證,則此方法必須以相同的順序返回元素。** 返回的數(shù)組將是“安全的”,因?yàn)榇思喜粫?huì)保留對(duì)它的引用。* (換句話說,即使此集合由數(shù)組支持,此方法也必須分配新數(shù)組)。* 調(diào)用者因此可以自由修改返回的數(shù)組。** @return 包含此集合中所有元素的數(shù)組*/Object[] toArray();/*** 返回包含此集合中所有元素的數(shù)組;* 返回?cái)?shù)組的運(yùn)行時(shí)類型是指定數(shù)組的運(yùn)行時(shí)類型。* 如果集合適合指定的數(shù)組,則返回元素到指定的數(shù)組中。* 否則,將使用指定數(shù)組的運(yùn)行時(shí)類型和此集合的大小分配新數(shù)組。** 此集合適合指定的數(shù)組,如果有空余空間(即,數(shù)組的元素多于此集合),則緊跟集合結(jié)尾的數(shù)組中的元素將設(shè)置為<tt> null </ TT>。* (如果調(diào)用者知道此集合不包含任何<tt> null </ tt>元素,則此選項(xiàng)僅用于確定此集合的長(zhǎng)度<i> </ i>。)** 如果此集合對(duì)其迭代器返回的元素的順序做出任何保證,則此方法必須以相同的順序返回元素。** @param <T> 包含集合的數(shù)組的運(yùn)行時(shí)類型* @throws ArrayStoreException 如果指定數(shù)組的運(yùn)行時(shí)類型不是此集合中每個(gè)元素的運(yùn)行時(shí)類型的超類型* @throws NullPointerException 如果指定的數(shù)組為null*/<T> T[] toArray(T[] a);// 修改操作/*** 將元素e添加到集合中* 如果集合不允許重復(fù)元素,且集合中已經(jīng)含有該元素,返回false* false-添加失敗*/boolean add(E e);/*** 從該 collection 中移除指定元素的單個(gè)實(shí)例,如果集合中存在指定元素返回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);/**** 刪除此集合中滿足給定布爾值函數(shù)的所有元素。在迭代期間或通過布爾值函數(shù)拋出的錯(cuò)誤或運(yùn)行時(shí)異常被中繼到調(diào)用者。* 默認(rèn)使用迭代器進(jìn)行刪除元素** @param filter 一個(gè)布爾值函數(shù),它返回{@code true}表示要?jiǎng)h除的元素* @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 與指定對(duì)象是否相等。通過覆蓋,實(shí)現(xiàn)list與list相等,set與set相等*/boolean equals(Object o);/*** 返回此 collection 的哈希碼值。*/int hashCode();/**** Spliterator是一個(gè)可分割迭代器(splitable iterator),可以和iterator順序遍歷迭代器一起看。jdk1.8發(fā)布后,對(duì)于并行處理的能力大大增強(qiáng),Spliterator就是為了并行遍歷元素而設(shè)計(jì)的一個(gè)迭代器,jdk1.8中的集合框架中的數(shù)據(jù)結(jié)構(gòu)都默認(rèn)實(shí)現(xiàn)了spliterator** 在此集合中的元素上創(chuàng)建{@link Spliterator}。** 后續(xù)如果講到了{(lán)@link Spliterator}再進(jìn)行補(bǔ)充下面的三個(gè)方法*** 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);}
}
?
總結(jié)
以上是生活随笔為你收集整理的Collection源码阅读的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dp打开思路3:HDU1069 POJ
- 下一篇: 使用nginx分片功能提升缓存效率,支持