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):它提供更具體的子接口(如 SetList)實現(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.
          <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.
          ?
          List接口
          List接口擴展了Collection接口。使用從0開始的索引,可以把一個元素插入到列表中或訪問列表中的某個元素。除了Collection接口中所定義的方法外,List接口還定義了屬于它自己的一些方法。如下圖:
          Method Summary
          ?booleanadd(E?o)
          ??????????Appends the specified element to the end of this list (optional operation).
          ?voidadd(int?index, E?element)
          ??????????Inserts the specified element at the specified position in this list (optional operation).
          ?booleanaddAll(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).
          ?booleanaddAll(int?index, Collection<? extends E>?c)
          ??????????Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
          ?voidclear()
          ??????????Removes all of the elements from this list (optional operation).
          ?booleancontains(Object?o)
          ??????????Returns true if this list contains the specified element.
          ?booleancontainsAll(Collection<?>?c)
          ??????????Returns true if this list contains all of the elements of the specified collection.
          ?booleanequals(Object?o)
          ??????????Compares the specified object with this list for equality.
          ?Eget(int?index)
          ??????????Returns the element at the specified position in this list.
          ?inthashCode()
          ??????????Returns the hash code value for this list.
          ?intindexOf(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.
          ?booleanisEmpty()
          ??????????Returns true if this list contains no elements.
          ?Iterator<E>iterator()
          ??????????Returns an iterator over the elements in this list in proper sequence.
          ?intlastIndexOf(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.
          ?Eremove(int?index)
          ??????????Removes the element at the specified position in this list (optional operation).
          ?booleanremove(Object?o)
          ??????????Removes the first occurrence in this list of the specified element (optional operation).
          ?booleanremoveAll(Collection<?>?c)
          ??????????Removes from this list all the elements that are contained in the specified collection (optional operation).
          ?booleanretainAll(Collection<?>?c)
          ??????????Retains only the elements in this list that are contained in the specified collection (optional operation).
          ?Eset(int?index, E?element)
          ??????????Replaces the element at the specified position in this list with the specified element (optional operation).
          ?intsize()
          ??????????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.
          <T> T[]
          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.
          ?Efirst()
          ??????????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.
          ?Elast()
          ??????????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

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          English

          JavaAPI

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宜丰县| 清水县| 商水县| 梅河口市| 大渡口区| 石渠县| 瑞昌市| 保山市| 安达市| 上高县| 蒙山县| 宝鸡市| 巴楚县| 沙田区| 双鸭山市| 孙吴县| 白城市| 临安市| 阳泉市| 积石山| 兴文县| 成武县| 阜南县| 临西县| 伊宁县| 义马市| 棋牌| 三亚市| 舒城县| 沐川县| 五寨县| 凤冈县| 馆陶县| 大余县| 邻水| 白银市| 南安市| 宾川县| 定襄县| 南皮县| 镇赉县|