搁置了好久了,今天抽点时间来整理下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.