原文出處:http://www.cnblogs.com/wishyouhappy/p/3669198.html
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> T[] toArray(T[] a)
2)Map介面(存放索引值對,Map中的值也可以是一個容器)
* Object get(Object key) * Object put(Object key, Object value) * Set keySet() : returns the keys set Set<K> keySet() * Set entrySet(): returns mappings set Set<Map.Entry<K,V>> 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 SortedMap<K,V>extends Map<K,V>
* Comparator comparator()
* Object firstKey()
* Object lastKey()
5)SortedSet
主要用於排序操作,實現此介面的子類都是排序的子類
public interface SortedSet<E>extends Set<E>
* Comparator comparator()
* E first() :返回第一個元素
* E last()
* SortedSet<E> headSet(E toElement): 返回less than toElement
* SortedSet<E> tailSet(E fromElement)
* SortedSet<E> subSet(E fromElement)
2.4抽象容器類
1)說明:使用抽象容器類可以方便的定義類,而不用在每個類中都實現容器介面container 中的所有的方法
2)包含:
* AbstractCollection public abstract class AbstractCollection<E>extends Objectimplements Collection<E> * AbstractList public abstract class AbstractList<E>extends AbstractCollection<E>implements List<E> * AbstractSet public abstract class AbstractSet<E>extends AbstractCollection<E>implements Set<E> * AbstactMap public abstract class AbstractMap<K,V>extends Object implements Map<K,V> * AbstractSequentialList public abstract class AbstractSequentialList<E> extends AbstractList<E>
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(均非同步,多線程時需要考慮安全執行緒問題),Vector(同步),Stack
1. List 介面支援通過索引的方法來訪問元素:ArrayList 隨機訪問快改慢;LinkedList改快隨機訪問慢;Vector實現了同步,因而比ArrayList慢
2. LinkedList使用雙向鏈表實現LinkedList提供額外的get,remove,insert方法在LinkedList的首部或尾部。這些操作使LinkedList可被用作堆棧(stack),隊列(queue)或雙向隊列(deque)。
3. ArrayList沒有定義增長演算法,當需要插入大量元素是,可調用ensureCapacity方法提高添加效率
4. Vector類似與ArrayList,但是是同步的,多安全執行緒(另外一點區別是ArrayList擴容時預設增長一半,Vector增長一倍)。無論是單線程還是多線程,Vector都比ArrayList慢
5. Stack繼承自Vector,實現一個後進先出的堆棧
6.若需要實現同步可以調用Collections工具類的synchronizedList方法,如下: