Java集合彙總(一)

java中,提供的集合關係:

首先感覺這張圖,開頭有點問題,如果Collection發出的虛線箭頭是實現關係那麼,不應該是Iterator,而應該是Iterable。


Collection類

Collection是集合類最基本的接口,List、Set都是實現Collection接口。

Collection中方法:

	public interface Collection<E> extends Iterable<E> {

	    /**
	     * 返回當前集合中的元素個數
	     */
	    int size();
	    
	    /**
	     *判斷當前集合是否爲空 
	     */
	    boolean isEmpty();

	    /**
	     * 判斷集合是否包含特定元素
	     */
	    boolean contains(Object o);

	    /**
	     * 返回用於訪問集合每個元素的迭代器
	     */
	    Iterator<E> iterator();

	    /**
	     * 將集合轉換爲數組
	     */
	    Object[] toArray();

	    /**
	     * 返回這個集合的對象數組,如果a足夠大,則將集合中元素全部放入a中,
	     * 否則重新分配一個數組。
	     */
	    <T> T[] toArray(T[] a);

	    // Modification Operations

	    /**
	     * 向集合中添加一個元素
	     */
	    boolean add(E e);

	    /**
	     * 從集合中刪除特定的元素
	     */
	    boolean remove(Object o);

	    /**
	     * 判斷當前集合是否包含c集合中的所有元素
	     */
	    boolean containsAll(Collection<?> c);

	    /**
	     * 將集合c中的全部元素添加到當前集合
	     */
	    boolean addAll(Collection<? extends E> c);

	    /**
	     * 將當前集合中包含集合c中的元素全部刪除
	     */
	    boolean removeAll(Collection<?> c);

	    /**
	     * 從當前集合刪除所有不在集合c中的元素
	     */
	    boolean retainAll(Collection<?> c);

	    /**
	     * 刪除當前集合中的所有元素
	     */
	    void clear();


	    // Comparison and hashing

	    /**
	     * 比較指定對象與當期集合是否相等
	     */
	    boolean equals(Object o);

	    /**
	     * Returns the hash code value for this collection.
	     */
	    int hashCode();
	}

List列表

他是一個有序,可重複對數組的擴展。用戶可以通過索引位置,查看元素。List作爲列表接口通常不直接使用,而是使用他的實現類LinkList、ArrayList、Vector。其中ArrayList和Vector是基於數組進行存儲的,LinkedList是基於鏈表存儲的。Vector是線程安全的,LinkList和ArrayList不是線程安全的。

public interface List<E> extends Collection<E> {
    /**
     * 返回列表中的長度
     */
    int size();
    /**
     * 判斷列表是否爲空
     */
    boolean isEmpty();
    /**
     * 判斷列表是否包含指定的對象
     */
    boolean contains(Object o);
    /**
     * 將列表轉換爲Iterator迭代器
     */
    Iterator<E> iterator();
    /**
     * 將列表轉換爲數組
     */
    Object[] toArray();
    /**
     * 將列表轉換爲數組,如果a足夠大,可以將其全部元素裝入。否則創建新的對象
     */
    <T> T[] toArray(T[] a);
    /**
     * 向列表中添加元素
     */
    boolean add(E e);
    /**
     * 從列表中移出元素
     */
    boolean remove(Object o);
    /**
     * 判斷列表中是否包含c集合中的所有元素
     */
    boolean containsAll(Collection<?> c);
    /**
     * 將集合內c的元素全部添加到列表中
     */
    boolean addAll(Collection<? extends E> c);
    /**
     * 從列表index位置,將集合c中所有元素全部添加
     */
    boolean addAll(int index, Collection<? extends E> c);
    /**
     * 從列表中刪除集合c中的元素
     */
    boolean removeAll(Collection<?> c);
    /**
     * 從當前列表中刪除不在c集合中的元素
     */
    boolean retainAll(Collection<?> c);
    /**
     * 清除列表中所有元素
     */
    void clear();
    /**
     * 判斷列表與指定對象是否相等
     */
    boolean equals(Object o);
    /**
     * 計算列表的哈希碼
     */
    int hashCode();
    /**
     * 根據位置index獲取列表中元素
     */
    E get(int index);
    /**
     * 將位置index元素,修改未element
     */
    E set(int index, E element);
    /**
     * 在index位置添加元素
     */
    void add(int index, E element);
    /**
     * 刪除指定位置的元素
     */
    E remove(int index);
    /**
     * 判斷元素o在列表中的位置
     */
    int indexOf(Object o);
    /**
     * 反方向查找元素o
     */
    int lastIndexOf(Object o);
    /**
     * 轉換爲可迭代的ListIterator
     */
    ListIterator<E> listIterator();
    /**
     * 同上,參數爲第一次調用next()返回指定位置的元素
     */
    ListIterator<E> listIterator(int index);
    /**
     * 截取列表中一段
     */
    List<E> subList(int fromIndex, int toIndex);
}

集合中爲List集合提供了ListIterator,該類實現了Iterator接口,可以訪問前一個元素和後一個元素

	public interface ListIterator<E> extends Iterator<E> {
	    /**
	     * 判斷是否還有下一個元素
	     */
	    boolean hasNext();
	    /**
	     * 獲取下一個元素
	     */
	    E next();
	    /**
	     * 判斷是否還有前一個元素
	     */
	    boolean hasPrevious();
	    /**
	     * 獲取前一個元素
	     */
	    E previous();
	    /**
	     * 獲取下一個元素位置
	     */
	    int nextIndex();
	    /**
	     * 獲取前一個元素位置
	     */
	    int previousIndex();
	    /**
	     * 刪除迭代器中元素
	     */
	    void remove();
	    /**
	     * 用新元素修改next或privious上次訪問的元素。如果沒有元素則拋出異常
	     */
	    void set(E e);
	    /**
	     * 在當前位置添加一個元素
	     */
	    void add(E e);
	}

LikeList鏈表

鏈表的優缺點:添加刪除操作比ArrayList高效,但是在隨機訪問方面就差很多了,這也是與ArrayList最大的區別。當確定使用鏈表時,應確保不會大量使用隨機訪問,而是使用經常使用插入,刪除操作。java中的鏈表都是雙向鏈接的,即每個節點都存儲着指向前驅和後集的引用。注意如果在LinkList中使用get(n)這個方法獲取元素,他的效率很低,因爲調用一次都是從頭遍歷過來。當在鏈表中如果使用這個方法的時候,就有可能選錯數據結構了。鏈表中訪問元素,使用迭代器。

boolean add(E e)
在列表末尾添加元素
void add(int index,E element)
在列表指定位置添加元素
boolean addAll(Collection<? extends E> c)
在鏈表之後添加集合元素,其實具體實現是調用的add(int index,Collection<? entends E> c),傳過去的index就是當期列表長度
boolean addAll(int index,Collection<? extends E> c)
在鏈表指定位置添加集合元素
void addFirst(E e)
在鏈表頭添加元素
void addLast(E e)
在鏈表之後添加元素
void clear()
清除鏈表中的所有元素
Object clone()
淺拷貝鏈表中的元素
boolean contains(Object o)
判斷列表中是否存在指定元素,其實源碼是調用indexOf()判讀位置實現的
Iterator<E> descendingIterator()
返回鏈表的迭代器
E element()
獲取當前列表元素,但不移出
E get(int index)
獲取指定位置的鏈表元素
E getFirst()
獲取鏈表表頭元素
E getLast()
獲取鏈表表尾元素
int indexOf(Object o)
返回指定元素在鏈表中的指定位置
int lastIndexOf(Object o)
返回鏈表中指定元素最後出現的位置
ListIterator<E> listIterator(int index)
返回listIterator迭代器,支持向前訪問
boolean offer(E e)
在鏈表尾部添加元素,內部實現其實調用的是add()方法
boolean offerFirst(E e)
在鏈表頭添加元素,內部實現就是調用addFirst();
boolean offerLast(E e)
在鏈表尾部添加元素,內部實現就是調用addLast();
E peek()
返回表頭元素,但不移出元素
E peekFirst()
返回表頭元素,但不移出元素
E peekLast()
返回表位元素,但不移出表位元素
E poll()
返回表頭元素,並且刪除表頭元素
E pollFirst()
返回表頭元素,並且刪除表頭元素
E pollLast()
返回表位元素,並且刪除表尾元素
E pop()
彈出鏈表中的元素
void push(E e)
向鏈表中壓入元素
E remove()
刪除表頭元素並且返回
E remove(int index)
刪除指定位置的元素,並且返回該位置的元素
boolean remove(Object o)
刪除第一次出現該元素的節點
E removeFirst()
刪除表頭元素,並且返回該元素
boolean removeFirstOccurrence(Object o)
從表頭開始遍歷到表尾,刪除該元素第一次出現的節點
E removeLast()
刪除並且返回鏈表最後一個元素
boolean removeLastOccurrence(Object o)
刪除指定元素在該鏈表最後一次出現的位置
E set(int index,E element)
將指定位置的元素修改爲指定元素
int size()
返回鏈表長度
Object[] toArray()
將鏈表轉換爲數組,其類型是Object類型
<T> T[] toArray(T[] a)
將鏈表轉換爲數組,類型是元素類型


在看鏈表源碼的時候,可以通過看看Node類的封裝,可以看出其存儲了前一個節點及後一個節點和節點元素:

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }



ArrayList數組列表

ArrayList是基於數組實現的,是一個動態數組,容量可以自增長。但他不是線程安全的,只能在單線程下運行,多線程環境下可以考慮Collections.synchronizedList(List l)返回一個線程安全的數組。

public class ArrayList<E> extends AbstractList<E>  implements List<E>, RandomAccess, Cloneable, java.io.Serializable
實現Serializable接口,因此支持序列化,能夠通過序列化遠程傳輸。實現Cloneable接口,因此支持克隆。實現RandonAccess,所以支持隨機訪問。


ArrayList源碼實現:

    private transient Object[] elementData;
    private int size;

elementData是存儲在數組列表中的元素,size是數組中的元素個數。transient是在序列化機制中,可能有些元素不想被序列化,使用該關鍵詞,可以去除序列化。關於序列化可以看我另一篇博客http://blog.sina.com.cn/s/blog_a36d34240102vkbb.html


public ArrayList(int size);
public ArrayList();
public ArrayList(Collection <? extends E> c);
ArrayList提供了三種構造方法,第一個指定特定長度,第二個是構造一個空的長度爲0的數組列表,第三個是構造一個含有指定collection元素的數組列表


提供的方法:

boolean add(E e)
在列表末尾添加元素
void add(int index,E element)
在列表指定位置添加元素
boolean addAll(Collection<? extends E> c)
將集合c中全部元素添加到列表末尾
boolean addAll(int index,Collection<? extends E> c)
向列表指定位置添加集合元素
void clear()
刪除數組列表中所有元素
Object clone()
克隆當前列表數組
boolean contains(Object o)
判斷當前元素是否包含在該列表中
void ensureCapacity(int minCapacity)
調整數組容量
E get(int index)
獲取指定位置的元素
int indexOf(Object o)
獲取指定對象的索引位置,如果該列表不存在該元素,則返回-1
boolean isEmpty()
判斷該列表是否爲空
Iterator<E> iterator()
返回該列表的迭代器
int lastIndexOf(Object o)
查找最後一個是指定對象o的索引位置,如果不存在,則返回-1
ListIterator<E> listIterator()
返回listIterator迭代器
ListIterator<E> listIterator(int index)
返回listIterator迭代器,下一個元素是指定位置的元素
E remove(int index)
刪除指定位置的元素
boolean remove(Object o)
刪除指定對象
boolean removeAll(Collection<?> c)
從列表中刪除包含集合c中的所有元素
protected void removeRange(int fromIndex, int toIndex)
刪除指定區間的元素
boolean retainAll(Collection<?> c)
刪除不包含在集合c中的元素
E set(int index,E element)
將指定位置的元素替換爲element
int size()
返回數組列表長度
List<E> subList(int fromIndex, int toIndex)
截取列表中指定位置的元素
Object[] toArray()
將列表轉換爲數組
<T> T[] toArray(T[] a)
將列表轉換爲數組,用a進行存儲,如果存儲不下則新建數組
void trimToSize()
調整底層數組,爲當前長度的數組

最好接合源碼進行查看可以查看http://www.cnblogs.com/ITtangtang/p/3948555.html、http://blog.csdn.net/jzhf2012/article/details/8540410寫的不錯


Vector

Vector的隨機訪問速度會比ArrayList慢,但是Vector是線程安全的,所以當需要多線程環境下還要考慮Vector。Vector中的方法使用synchronized修飾

他也是實現了可增長的數組,維持着capacityIncrement變量,capacityIncrement爲每次增長的容量。整體實現原理與ArrayList一樣,但是全程是線程安全的

  /**
   *initalCapacity爲初始化數組長度,capacityIncrement爲自動增長的倍數
   */ 
   public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

    /**
     */
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

    /**
     * 無參構造方法,默認長度是10
     */
    public Vector() {
        this(10);
    }

    /**
     * 將集合元素初始化到Vector中
     */
    public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

boolean add(E e)
Appends the specified element to the end of this Vector.
void add(int index, E element)
Inserts the specified element at the specified position in this Vector.
boolean addAll(Collection<? extends E> c)
Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's Iterator.
boolean addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified Collection into this Vector at the specified position.
void addElement(E obj)
Adds the specified component to the end of this vector, increasing its size by one.
int capacity()
Returns the current capacity of this vector.
void clear()
Removes all of the elements from this Vector.
Object clone()
Returns a clone of this vector.
boolean contains(Object o)
Returns true if this vector contains the specified element.
boolean containsAll(Collection<?> c)
Returns true if this Vector contains all of the elements in the specified Collection.
void copyInto(Object[] anArray)
Copies the components of this vector into the specified array.
E elementAt(int index)
Returns the component at the specified index.
Enumeration<E> elements()
Returns an enumeration of the components of this vector.
void ensureCapacity(int minCapacity)
Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
boolean equals(Object o)
Compares the specified Object with this Vector for equality.
E firstElement()
Returns the first component (the item at index 0) of this vector.
E get(int index)
Returns the element at the specified position in this Vector.
int hashCode()
Returns the hash code value for this Vector.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
int indexOf(Object o, int index)
Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
void insertElementAt(E obj, int index)
Inserts the specified object as a component in this vector at the specified index.
boolean isEmpty()
Tests if this vector has no components.
Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
E lastElement()
Returns the last component of the vector.
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
int lastIndexOf(Object o, int index)
Returns the index of the last occurrence of the specified element in this vector, searching backwards from index, or returns -1 if the element is not found.
ListIterator<E> listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E remove(int index)
Removes the element at the specified position in this Vector.
boolean remove(Object o)
Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
boolean removeAll(Collection<?> c)
Removes from this Vector all of its elements that are contained in the specified Collection.
void removeAllElements()
Removes all components from this vector and sets its size to zero.
boolean removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this vector.
void removeElementAt(int index)
Deletes the component at the specified index.
protected void removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
boolean retainAll(Collection<?> c)
Retains only the elements in this Vector that are contained in the specified Collection.
E set(int index, E element)
Replaces the element at the specified position in this Vector with the specified element.
void setElementAt(E obj, int index)
Sets the component at the specified index of this vector to be the specified object.
void setSize(int newSize)
Sets the size of this vector.
int size()
Returns the number of components in this vector.
List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
Object[] toArray()
Returns an array containing all of the elements in this Vector in the correct order.
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.
String toString()
Returns a string representation of this Vector, containing the String representation of each element.
void trimToSize()
Trims the capacity of this vector to be the vector's current size.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章