java容器类总结(一)

2014-11-23 23:13:43 · 作者: · 浏览: 0
1.java容器分类图
  说明:左图为简化图(其中粗线部分是重点的容器),右图为完整容器分类图
2.容器类接口和抽象容器类
2.1 说明
  容器接口是容器的基础。使用接口可以将容器的实现与容器接口分开,因而可以使用相同的方法访问容器而不需关心容器具体的数据结构。
  同理,Iterator接口也使用户能够使用相同的方法访问不同的容器类。
2.2 容器接口(Collection,Map,Iterator)
  1)collection接口
复制代码
    * boolean add(Object obj): 添加对象,集合发生变化则返回true
    * Iterator iterator():返回Iterator接口的对象
    * int size()
    * boolean isEmpty()
    * boolean contains(Object obj)
    * void clear()
    * T[] toArray(T[] a)
复制代码
  2)Map接口(存放键值对,Map中的值也可以是一个容器)
复制代码
    * Object get(Object key)
    * Object put(Object key, Object value)
    * Set keySet() : returns the keys set Set keySet()
    * Set entrySet(): returns mappings set Set> entrySet()
    * containsKey()
    * containsValue()
复制代码
  3)Iterator接口
    * Object next()
    * boolean hasNext()
    * void remove()
  注意:remove函数不能连续执行多次,否则返回IllegalStateException
( if the next method has not yet been called, or the remove method has already been called after the last call to the next method.)
通常用法:
  Iterator it=collection.iterator();
    while(it.hasNext())
    {
     Object obj=it.next();
    //do something
    }
2.3 子接口(List,Set,ListIterator,SortedMap,SortedSet)
  1)List(有顺序可以重复,有顺序所以操作时可以在方法中加入索引参数,如下:)
  * boolean add(E element)
  * void add(int index, E element)
  * E set(int index, E element)
  * E get(int index);
  2)Set(无顺序不可以重复,无序因而不能通过索引操作对象)
  3)ListIterator(Iterator for List,List是双向表,因而在Iterator上增加了一些新的方法,允许traverse the List in either direction)
  * boolean hasPrevious();
  * E previous();
  * int previousIndex()
  4) SortedMap
  说明:保证按照键的升序排列的映射,可以按照键的自然顺序( Comparable 接口)进行排序, 或者通过创建有序映射时提供的比较器进行排序
  (A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time)
  public interface SortedMapextends Map
  * Comparator comparator()
  * Object firstKey()
  * Object lastKey()
  5)SortedSet 
  主要用于排序操作,实现此接口的子类都是排序的子类
复制代码
public interface SortedSetextends Set
  * Comparator comparator()
  * E first() :返回第一个元素
  * E last()
* SortedSet headSet(E toElement): 返回less than toElement
  * SortedSet tailSet(E fromElement)
  * SortedSet subSet(E fromElement)
复制代码
2.4抽象容器类
  1)说明:使用抽象容器类可以方便的定义类,而不用在每个类中都实现容器接口container 中的所有的方法
  2)包含:
  * AbstractCollection     public abstract class AbstractCollectionextends Objectimplements Collection
  * AbstractList public abstract class AbstractListextends AbstractCollectionimplements List
  * AbstractSet        public abstract class AbstractSetextends AbstractCollectionimplements Set
  * AbstactMap public abstract class AbstractMapextends Object implements Map
  * AbstractSequentialList public abstract class AbstractSequentialList extends AbstractList
3.具体容器类
3.1概括
1)collection: ArrayList,LinkedLsit,Vector,Stack
       TreeSet,HashSet,LinkedHashSet
2) Map:    HashMap,LinkedHashMap,WeakHashMap, TreeMap, HashTable, IdentityHashTable(其中key的比较是通过==而不是equals)
3.2常用的容器类
1)ArrayList 与 LinkedList(均非同步,多线程时需要考虑线程安全问题),V