擱置了好久了,今天抽點時間來整理下java集合框架。
首先,是整體框架體系圖:
集合兩個頂級接口爲Collection和Map。
Collection
以下是官方的api文檔:(地址:點擊打開鏈接)
Modifier and Type | Method and Description |
---|---|
boolean | add(E e) Ensures that this collection contains the specified element (optional operation). |
boolean | addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this collection (optional operation). |
void | clear() Removes all of the elements from this collection (optional operation). |
boolean | contains(Object o) Returns true if this collection contains the specified element. |
boolean | containsAll(Collection<?> c) Returns true if this collection contains all of the elements in the specified collection. |
boolean | equals(Object o) Compares the specified object with this collection for equality. |
int | hashCode() Returns the hash code value for this collection. |
boolean | isEmpty() Returns true if this collection contains no elements. |
Iterator<E> | iterator() Returns an iterator over the elements in this collection. |
boolean | remove(Object o) Removes a single instance of the specified element from this collection, if it is present (optional operation). |
boolean | removeAll(Collection<?> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
boolean | retainAll(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation). |
int | size() Returns the number of elements in this collection. |
Object[] | toArray() Returns an array containing all of the elements in this collection. |
<T> T[] | toArray(T[] a) Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
不做詳述,用到了再具體說明。
Collection接口下面有3個常用子接口:List,Set,Queue。
List
一個 List 是一個元素有序的、可以重複、可以爲 null 的集合(有時候我們也叫它“序列”)。
這裏的有序,指的是存在每一個下標,有唯一的一個元素與之相對應。並非是集合按順序排列!參見官方說明:
An ordered collection (also known as a <i>sequence</i>). The user of this * interface has precise control over where in the list each element is * inserted. The user can access elements by their integer index (position in * the list), and search for elements in the list.
Set
一個Set是一個元素無序、不能重複、只有一個元素能爲null的集合。
見官方說明:
* A collection that contains no duplicate elements. More formally, sets * contain no pair of elements <code>e1</code> and <code>e2</code> such that * <code>e1.equals(e2)</code>, and at most one null element. As implied by * its name, this interface models the mathematical <i>set</i> abstraction.
Queue
隊列是一種特殊的線性表,它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作。進行插入操作的端稱爲隊尾,進行刪除操作的端稱爲隊頭。隊列中沒有元素時,稱爲空隊列。
在隊列這種數據結構中,最先插入的元素將是最先被刪除的元素;反之最後插入的元素將是最後被刪除的元素,因此隊列又稱爲“先進先出”(FIFO—first in first out)的線性表。
Queue使用時要儘量避免Collection的add()和remove()方法,而是要使用offer()來加入元素,使用poll()來獲取並移出元素。它們的優點是通過返回值可以判斷成功與否,add()和remove()方法在失敗的時候會拋出異常。 如果要使用前端而不移出該元素,使用element()或者peek()方法。
以下是官方文檔:
* A collection designed for holding elements prior to processing. * Besides basic {@link java.util.Collection Collection} operations, * queues provide additional insertion, extraction, and inspection * operations. Each of these methods exists in two forms: one throws * an exception if the operation fails, the other returns a special * value (either {@code null} or {@code false}, depending on the * operation). The latter form of the insert operation is designed * specifically for use with capacity-restricted {@code Queue} * implementations; in most implementations, insert operations cannot * fail.