java.util包学习(二)集合框架(接口)

集合框架的设计目标
(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 不提供此接口的任何直接 实现:它提供更具体的子接口(如 SetList)实现。
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接口并定义这样一种集合:这种集合不允许含有相同的元素。因此,如果试图插入相同的元素到调用集合中,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 肖麦 阅读(525) 评论(0)  编辑  收藏 所属分类: JavaAPI


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

English

JavaAPI

搜索

最新评论

阅读排行榜

评论排行榜