java.util包學(xué)習(xí)(二)集合框架(接口)
集合框架的設(shè)計目標
(1)基本集合(動態(tài)數(shù)組、鏈表、樹、和散列表)的實現(xiàn)具有很高的效率
(2)這個框架必須允許不同類型的集合用相似的方式進行處理且具有很高的互用性
(3)擴展與修改集合必須是容易的.為了實現(xiàn)這一目標,整個集合框架被設(shè)計成一系列的標準接口,并且實用工具包中提供了這些接口的幾個標準實現(xiàn),如LinkedList、HashSet和TreeSet。
(4)允許程序員把標準數(shù)組集成到集合結(jié)構(gòu)中的機制
集合框架的Iterator(迭代器)接口:
Iterator提供了訪問集合元素的一種標準訪問方式:逐個訪問方式。由于每個集合均實現(xiàn)Iterator接口,所以任何集合類的成員均可通過Iterator類的方法訪問
集合框架的接口:
下面將介紹集合框架的每個接口。首先從集合接口開始,因為它們決定集合類的基本特性
- Collection????????? 允許處理一組對象,位于集合分層結(jié)構(gòu)的頂層
- List????????????????????擴展Collection接口來處理序列(即對象序列)
- Set????????????????????擴展Collection接口來處理對象集,其中集合元素必須是惟一的
- SortedSet????????? 擴展Set接口來處理已排序的對象集
除了Collection外,集合中還有Comparator、Iterator、ListIterator和RandomAccess接口。
Comparator定義怎樣比較兩個對象;Iterator和ListIterator列舉集合中的對象;而通過實現(xiàn)RandomAccess,列表允許對其元素進行有效的隨機訪問。
Collection接口:
Collection接口是構(gòu)造集合框架的基礎(chǔ),并聲明了各種集合所共有的核心方法。JDK 不提供此接口的任何直接 實現(xiàn):它提供更具體的子接口(如 Set 和 List)實現(xiàn)。
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接口并定義這樣一種集合:這種集合不允許含有相同的元素。因此,如果試圖插入相同的元素到調(diào)用集合中,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. |
posted on 2007-03-28 10:54 肖麥 閱讀(551) 評論(0) 編輯 收藏 所屬分類: JavaAPI