“連接池(Connection接口)”這一概念就是數據庫服務器的一個開放連接集。
集可以是有限的,也可以是無限的。
“集合框架”由一組用來操作對象的接口組成。
在“集合框架”中,接口Map和Collection在層次結構沒有任何親緣關係,它們是截然不同的(Map的典型應用是訪問按關鍵字存儲的值。它支持一系列集合操作的全部,但操作的是鍵-值對,而不是單個獨立的元素)。返回Map對象的Set視圖的方法:
Setset = aMap.keySet();
“集合框架”四個基本接口的層次結構:
Collection接口是一組允許重複的對象。
Set接口繼承Collection,但不允許重複。
List接口繼承Collection,允許重複,並引入位置下標。
Map接口既不繼承Set也不繼承Collection。
接口 |
實現 |
歷史集合類 |
Set |
HashSet |
|
|
TreeSet |
|
List |
ArrayList |
Vector |
|
LinkedList |
Stack |
Map |
HashMap
|
Hashtable |
|
TreeMap
|
Properties
|
Collection集合接口
public interface Collection<E> extends Iterable<E> {
/** Query Operations **/
// Returns the number of elements in this collection.
int size();
// Returns “true” if this collection contains no elements.
boolean isEmpty();
// Returns <tt>true</tt> if this collection contains the specified element.
boolean contains(Object o);
// Returns an iterator over the elements in this collection.
Iterator<E> iterator();
// Returns an array containing all of the elements in this collection.
Object[] toArray();
// Returns an array containing all of the elements in this collection;
// the runtime type of the returned array is that of the specified array.
<T> T[] toArray(T[] a);
/** Modification Operations **/
// Ensures that this collection contains the specified element (optional
// operation). Returns <tt>true</tt> if this collection changed as a
// result of the call.
boolean add(E e);
// Removes a single instance of the specified element from this collection。
boolean remove(Object o);
/** Bulk Operations 組操作 **/
// Returns <tt>true</tt> if this collection contains all of the elements in the specified collection.允許您查找當前集合是否包含了另一個集合的所有元素,即另一個集合是否是當前集合的子集。
boolean containsAll(Collection<?> c);
// Adds all of the elements in the specified collection to this collection(optional operation).
boolean addAll(Collection<? extends E> c);
// Removes all of this collection's elements that are also contained in the specified collection(optional operation).public abstract Iterator<E> iterator();
public abstract int size();
boolean removeAll(Collection<?> c);
// Retains(保持,記住) only the elements in this collection that are contained in the specified collection(optional operation).從當前集合中除去不屬於另一個集合的
素,即交。
boolean retainAll(Collection<?> c);
// Removes all of the elements from this collection(optional operation).
void clear();
/** Comparison and hashing **/
// Compares the specified object with this collection for equality.
boolean equals(Object o);
// Returns the hash code value for this collection.
int hashCode();
}
Collection接口的iterator()方法返回一個Iterator。使用Iterator接口方法,您可以從頭至尾遍歷集合,並安全的從底層
Collection中除去元素:
remove()方法可由底層集合有選擇的支持。當底層集合調用並支持該方法時,最近一次next()調用返回的元素就被除去。如下面代碼:
Collection collection = ...;
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
if (removalCheck(element)) {
iterator.remove();
}
}
AbstractCollection類(抽象類)
public abstract class AbstractCollection<E> implements Collection<E> {
/** AbstractCollection 類提供具體“集合框架”類的基本功能。實現了大多數Collection接口中的方法。還有幾個添加的方法。雖然您可以自行實現 Collection 接口的所有方法,但是,除
了iterator() 和 size() 方法在恰當的子類中實現以外,其它所有方法都由 AbstractCollection 類來提供實現。
public abstract Iterator<E> iterator();
public abstract int size();
就剩下這兩個方法沒有提供實現,其他的都提供了實現代碼 **/
}
我們看到Collection接口中有許多方法是可選的optionaloperation,假如在運行過程中拋出UnsupportedOperationException異常,則操作失敗,說明這個方法不被支持。
Set接口
Set接口繼承Collection接口,沒有添加新的方法。依賴添加對象方法add()中用到了equals()方法類確保唯一性。
publicinterfaceSet<E>extendsCollection<E> {
/**和Collection接口中的方法是一樣一樣的**/
}
“集合框架”中提供了兩個Set接口的實現類HashSet類和TreeSet類:
HashSet類
考慮到效率,添加到HashSet的對象採用恰當的方式來實現來實現hashCode()方法,雖然大多數系統類都是覆蓋了Object的hashCode()方法;但是,當創建您自己的要添加到HashSet中的類時,別忘了覆蓋Object的hashCode()方法。
package java.util;
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
……
}
TreeSet類
當你要從集合中以有序的方式抽取元素時,我們可以採用TreeSet實現會有好處。
爲優化HashSet空間的使用,您可以調優初始容量和負載因子。TreeSet不包含調優選項,因爲樹總是平衡的,保證了插入、刪
除、查詢的性能爲log(n)。
package java.util;
public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable
{
……
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
……
}
我們可以看到HashSet類和TreeSet類都實現了AbstrctSet類,下面就來看看AbstractSet類(抽象類)做了什麼:
AbstractSet類(抽象類)
AbstractSet類覆蓋了equals()和hashCode()方法,以確保兩個相等的集返回相同的散列碼。若兩個集大小相等且包含相同元素,則這兩個集相等。按定義,集散列碼是集中元素散列碼的總和。因此,不論集的內部順序如何,兩個相等的集會報告相同的散列碼。
package java.util;
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
protected AbstractSet() {
}
public boolean equals(Object o) {
……
}
public int hashCode() {
……
}
public boolean removeAll(Collection<?> c) {
……
}
}
List接口
List接口繼承了Collection接口,定義了一個允許重複項的的有序集合。還添加了一些方法:面向位置的操作;處理集合子類。
package java.util;
public interface List<E> extends Collection<E> {
// 面向位置的操作
void add(int index, Object element)
boolean addAll(int index, Collection collection)
Object get(int index)
int indexOf(Object element)
int lastIndexOf(Object element)
Object remove(int index)
Object set(int index, Object element)
// 集合子集
ListIterator listIterator()
ListIterator listIterator(int startIndex)
List subList(int fromIndex, int toIndex)
}
上面的代碼中出現了ListIterator,來了解一下:
閱讀ListIterator接口的定義源碼發現ListIterator接口中多了幾個方法:
booleanhasPrevious();
Eprevious();
intpreviousIndex();
由此可見,它是支持雙向訪問的。
LinkList/ArrayList
在“集合框架”中有兩個List接口的實現類:ArrayList類和LinkList類
ArrayList類:隨即訪問,而不必在除尾部外的任何位置插入或去除元素。
LinkList類:需要頻繁的在列表中插入或去除元素,只需要順序訪問。添加了一些處理鏈表兩端的方法,這樣可以輕鬆的作爲堆棧和隊列。
AbstractList/AbstractSequentialList
在“集合框架”中有兩個List接口的抽象實現類:AbstractList類和AbstractSequentialList類