Collection
The root interface in the collection hierarchy. A collection represents a group of objects, known as its <i>elements</i>.
一些集合允許元素重復,一些則不然.一些集合是有序的,一些是無序的.JDK沒有提供該接口的直接實現,所有的實現都是基于Collection的子接口進行的實現,比如Set,List等等.
Collection默認提供兩個標準的構造函數:
1.沒有參數的構造函數,創建一個空的集合類
2.有一個類型與基類(Collection或Map)相同的構造函數,創建一個與給定參數具有相同元素的新集合類
- 怎么來講,Collection就像一個封裝了一個微型的數據庫,也可以稱之為一個容器,對外提供了增刪改查的接口,同時,為我們封裝好了這個容器應該具有的一些其他操作,比如獲取這個容器的大小.容器是否為空等等一些所有容器類東西的通用操作.
- 這些容器不僅限于一下:Set,List,Map,SortedSet,SortedMap,HashSet,TreeSet,ArrayList,LinkedList,Vector,Collections, Arrays, AbstractCollection.上述的容器不一定實現了Collection接口,但是他們都會很好的提供自身與Collection轉化的方法,一般都會包含Collection,比如AbstractHashMap如下圖.
AbstractHashMap
- 設計理念:
Collection提供了一套小而美的API,API接口需要對程序員友好,增加新功能時,能讓程序員快速上手.
Collection與Map接口不會區分該集合是否可變(mutability),是否可更改(modifiability),是否可改變大小(resizability)這些細微的差別.相反,一些操作是可選的,在實現時拋出UnsupportedOperationException即可表示集合不支持該操作。集合的實現者必須在文檔中聲明那些操作是不支持的。
- 類圖:
那么,一個一個來:
Set
Iterator
package java.util;
/**
集合之上的Iterator,是所有集合實現的一個接口
設計目的:抽象遍歷集合的邏輯,避免向用戶暴露集合內部過多的結構與實現
*/
public interface Iterator<E> {
/**當前遍歷器有下一個元素是,返回true;以免拋出異常
*/
boolean hasNext();
/**
返回下一個元素;如果沒有,則拋出NoSuchElementException異常
*/
E next();
/**
移除當前遍歷器中的最后一個元素,每次使用next()方法,只能使用一次該方法。
當前迭代器有對集合修改的動作時,該集合的遍歷器的行為是不確定的。這句話有些抽象,
可以下面的example 1。
同樣,該方法用了java8的特性,default-虛擬擴展方法
使用目的:在該接口中進行默認實現,提高接口的擴展性,避免在接口擴展的時候,破壞原有的實現
*/
default void remove();
}
Iterable
/*實現該接口的object可以使用“for-each loop”表達式*/
public interface Iterable<T> {
/*返回一個基于泛型T類型的Iterator*/
Iterator<T> iterator();
/**
對每一個元素執行給出的動作。
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
/**
* Creates a {@link Spliterator} over the elements described by this
* {@code Iterable}.
*
* @implSpec
* The default implementation creates an
* <em><a href="Spliterator.html#binding">early-binding</a></em>
* spliterator from the iterable's {@code Iterator}. The spliterator
* inherits the <em>fail-fast</em> properties of the iterable's iterator.
*
* @implNote
* The default implementation should usually be overridden. The
* spliterator returned by the default implementation has poor splitting
* capabilities, is unsized, and does not report any spliterator
* characteristics. Implementing classes can nearly always provide a
* better implementation.
*
* @return a {@code Spliterator} over the elements described by this
* {@code Iterable}.
* @since 1.8
*/
default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}
ListIterator
package java.util;
/**
Iterator接口的擴展,為list實現的雙向遍歷,并且能獲取到當前遍歷的位置,
*/
public interface ListIterator<E> extends Iterator<E> {
boolean hasNext();
E next();
/**
是否有上一個參數。詳細參考一下鏈接: http://sungang-1120.iteye.com/blog/1747384
*/
boolean hasPrevious();
/*返回上一個元素*/
E previous();
/**返回下一個元素的坐標,直到返回list.size
*/
int nextIndex()
/**
返回上一個坐標,直到返回-1
*/
int previousIndex();
/**
移除剛剛指向的元素(next()或者previous()操作之后的指向的那個元素)
*/
void remove()
/**
元素插入到list末尾
*/
void set(E e);
/**
插入元素到列表頭部,詳細參考:http://www.cnblogs.com/baiqiantao/p/5449434.html
*/
void add(E e);
}
Vector
構造方法:
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public Vector() {
this(10);
}
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
/×將vetcor中的元素拷貝到anArray數組中,同步方法×/
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
/×將當前數組的容量改為當前數組數據數量的大小×/`
public synchronized void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (elementCount < oldCapacity) {
elementData = Arrays.copyOf(elementData, elementCount);
}
}
AbstractList
/*在對一個集合對象進行跌代操作的同時,并不限制對集合對象的元素進行操 作,這些操作包括一些可能引起
跌代錯誤的add()或remove()等危險操作。在AbstractList中,使用了一個簡單的機制來規避這些風險。 這就是
modCount(存在于AbstractList)和expectedModCount(存在于該類實現的接口iterator中)的作用所在。詳
細參考:http://liuzhaomin.iteye.com/blog/1038289*/
protected transient int modCount = 0;