“连接池(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集合接口
[java]
public interface Collection extends Iterable {
/** Query Operations **/
// Returns the number of elements in this collection.
int size();
// Returns “true” if this collection contains no elements.
boolean isEmpty();
// Returns true if this collection contains the specified element.
boolean contains(Object o);
// Returns an iterator over the elements in this collection.
Iterator 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.
/** Modification Operations **/
// Ensures that this collection contains the specified element (optional
// operation). Returns true 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 true 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 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()调用返回的元素就被除去。如下面代码:
[java]
Collection collection = ...;
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
if (removalCheck(element)) {
iterator.remove();
}
}
AbstractCollection类(抽象类)
[java]
public abstr