集合框架的设计目标
(1)基本集合(动态数组、链表、树、和散列表)的实现具有很高的效率
(2)这个框架必须允许不同类型的集合用相似的方式进行处理且具有很高的互用性
(3)扩展与修改集合必须是容易的.为了实现这一目标,整个集合框架被设计成一系列的标准接口,并且实用工具包中提供了这些接口的几个标准实现,如LinkedList、HashSet和TreeSet。
(4)允许程序员把标准数组集成到集合结构中的机制
集合框架的Iterator(迭代器)接口:
Iterator提供了访问集合元素的一种标准访问方式:逐个访问方式。由于每个集合均实现Iterator接口,所以任何集合类的成员均可通过Iterator类的方法访问
集合框架的接口:
下面将介绍集合框架的每个接口。首先从集合接口开始,因为它们决定集合类的基本特性
- Collection 允许处理一组对象,位于集合分层结构的顶层
- List 扩展Collection接口来处理序列(即对象序列)
- Set 扩展Collection接口来处理对象集,其中集合元素必须是惟一的
- SortedSet 扩展Set接口来处理已排序的对象集
除了Collection外,集合中还有Comparator、Iterator、ListIterator和RandomAccess接口。
Comparator定义怎样比较两个对象;Iterator和ListIterator列举集合中的对象;而通过实现RandomAccess,列表允许对其元素进行有效的随机访问。
Collection接口:
Collection接口是构造集合框架的基础,并声明了各种集合所共有的核心方法。JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。
Collection接口中的方法:
Method Summary | ||
---|---|---|
boolean |
add (E o) 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 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. |
|
|
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. |
List接口
List接口扩展了Collection接口。使用从0开始的索引,可以把一个元素插入到列表中或访问列表中的某个元素。除了Collection接口中所定义的方法外,List接口还定义了属于它自己的一些方法。如下图:
Method Summary | ||
---|---|---|
boolean |
add(E o) Appends the specified element to the end of this list (optional operation). |
|
void |
add(int index, E element) Inserts the specified element at the specified position in this list (optional operation). |
|
boolean |
addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). |
|
boolean |
addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list at the specified position (optional operation). |
|
void |
clear() Removes all of the elements from this list (optional operation). |
|
boolean |
contains(Object o) Returns true if this list contains the specified element. |
|
boolean |
containsAll(Collection<?> c) Returns true if this list contains all of the elements of the specified collection. |
|
boolean |
equals(Object o) Compares the specified object with this list for equality. |
|
E |
get(int index) Returns the element at the specified position in this list. |
|
int |
hashCode() Returns the hash code value for this list. |
|
int |
indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. |
|
boolean |
isEmpty() Returns true if this list contains no elements. |
|
Iterator<E> |
iterator() Returns an iterator over the elements in this list in proper sequence. |
|
int |
lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. |
|
ListIterator<E> |
listIterator() Returns a list iterator of the elements in this list (in proper sequence). |
|
ListIterator<E> |
listIterator(int index) Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. |
|
E |
remove(int index) Removes the element at the specified position in this list (optional operation). |
|
boolean |
remove(Object o) Removes the first occurrence in this list of the specified element (optional operation). |
|
boolean |
removeAll(Collection<?> c) Removes from this list all the elements that are contained in the specified collection (optional operation). |
|
boolean |
retainAll(Collection<?> c) Retains only the elements in this list that are contained in the specified collection (optional operation). |
|
E |
set(int index, E element) Replaces the element at the specified position in this list with the specified element (optional operation). |
|
int |
size() Returns the number of elements in this list. |
|
List<E> |
subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
|
Object[] |
toArray() Returns an array containing all of the elements in this list in proper sequence. |
|
|
toArray(T[] a) Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. |
Set接口
Set接口扩展Set接口并定义这样一种集合:这种集合不允许含有相同的元素。因此,如果试图插入相同的元素到调用集合中,add()将会返回false。Set接口没有定义它自己的方法。所有方法都继承自Collection接口。
Sorted接口
SortedSet接口扩展Collection接口并定义一种升序集合。除了Set接口所定义的方法之外,SortedSet接口还定义了小表所给出的方法,以便集合的操作变得更为方便。
Method Summary | |
---|---|
Comparator<? super E> |
comparator() Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering. |
E |
first() Returns the first (lowest) element currently in this sorted set. |
SortedSet<E> |
headSet(E toElement) Returns a view of the portion of this sorted set whose elements are strictly less than toElement. |
E |
last() Returns the last (highest) element currently in this sorted set. |
SortedSet<E> |
subSet(E fromElement, E toElement) Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive. |
SortedSet<E> |
tailSet(E fromElement) Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement. |