1. 程式人生 > >Java資料結構-Collection(一)

Java資料結構-Collection(一)

Java中集合的框架圖

Collection is the root of the collection hierarchy. It defines operations on data collections and the behavior that they will have in all implementations of Collections. All direct or indirect implementations of Collection should implement at least two constructors. One with no parameters which creates an empty collection and one with a parameter of type 

Collection. This second constructor can be used to create a collection of different type as the initial collection but with the same elements. Implementations of Collection cannot be forced to implement these two constructors but at least all implementations under java.util do. Methods that change the content of a collection throw an 
UnsupportedOperationException if the underlying collection does not support that operation, though it's not mandatory to throw such an Exception in cases where the requested operation would not change the collection. In these cases it's up to the implementation whether it throws an UnsupportedOperationException 
or not. Methods marked with (optional) can throw an UnsupportedOperationException if the underlying collection doesn't support that method.

collection 是collection體系中的根介面,它定義了所有實現該介面的類的資料集合的操作和行為。所有直接或者間接實現該介面的類應該實現至少兩個建構函式。一個無參的函式用來建立一個空的collection,一個函式接收一個型別引數。第二個函式可以建立一個collection用來儲存與引數宣告的型別一致的資料。collection的實現類不強制要求必須實現這兩個建構函式,但是java.util包裡的實現類都這樣做了。改變collection內容的操作可能導致UnsupportedOperationException異常,如果collection底層不支援這種操作的話。在請求的操作不會如預期一樣改變collection的時候,它不會強制是否丟擲一個異常。在這些情況下,由實現類決定是否丟擲UnsupportedOperationException異常。帶(optional)標記的函式是可選擇實現的方法,可能由於未實現而導致UnsupportedOperationException異常。

collection介面繼承iterable介面。iterable介面實現類可以進行增強for迴圈。

publicinterface Iterable<T> {

    /**

     * Returns an {@link Iterator} for the elements in this object.

    *

    * @return An {@code Iterator} instance.

     */

    Iterator<T> iterator();

}

collection的子類:

collection方法概述:

public abstract boolean add (E object)

Attempts to add object to the contents of this Collection (optional). After this method finishes successfully it is guaranteed that the object is contained in the collection. If the collection was modified it returns truefalse if no changes were made. An implementation of Collection may narrow the set of accepted objects, but it has to specify this in the documentation. If the object to be added does not meet this restriction, then an IllegalArgumentException is thrown. If a collection does not yet contain an object that is to be added and adding the object fails, this method must throw an appropriate unchecked Exception. Returning false is not permitted in this case because it would violate the postcondition that the element will be part of the collection after this method finishes.

該方法嘗試將物件加入collection中。該方法成功呼叫結束後將會保證被新增的物件包含在collection中。如果collection被修改了,該方法將會反悔true,如果collection沒有變化,將會返回false。collection的實現類可能會限制可以接收的物件的範圍,但是必須在文件中申明。如果被新增的物件不符合限制要求的話,就會丟擲IllegalArgumentException異常。如果一個collection沒有包含要被新增的物件,而且新增該物件失敗了,那麼必須丟擲 appropriate unchecked Exception異常。這種情況下不被允許返回false,因為它可能會破壞後來的結果-方法結束後該物件成為了collection的一部分。

Parameters
object the object to add.
Returns
  • true if this Collection is modified, false otherwise.
Throws
if the class of the object is inappropriate for this collection.
if the object cannot be added to this Collection.
if null elements cannot be added to the Collection.

public abstract boolean addAll (Collection<? extends E> collection)

Attempts to add all of the objects contained in Collection to the contents of this Collection (optional). If the passed Collection is changed during the process of adding elements to this Collection, the behavior is not defined.

該方法將嘗試把另外一個collection中包含的所有物件新增到此collection中。如果在此過程中,被新增的collection發生了改變,這種情形沒有做出定義。(保證在此過程中被新增的collection未被改變。)

Parameters

collection the Collection of objects.
Returns
  • true if this Collection is modified, false otherwise.
Throws
if the class of an object is inappropriate for this Collection.
if an object cannot be added to this Collection.
if collection is null, or if it contains null elements and this Collection does not support such elements.

public abstract void clear ()

Removes all elements from this Collection, leaving it empty (optional).

移除該collection中的所有元素,使其為空。
Throws

public abstract boolean contains 

Tests whether this Collection contains the specified object. Returns true if and only if at least one element elem in this Collection meets following requirement:(object==null ? elem==null : object.equals(elem)).

測試該collection是否包含特定的物件。當且僅當至少一個collection中的元素滿足一下條件時(如果特定物件為null時,collection中有null元素,特定物件不為空時,該物件通過equals方法與元素比較後返回true)返回true。
Parameters
object the object to search for.
Returns
  • true if object is an element of this Collectionfalse otherwise.
Throws
if the object to look for isn't of the correct type.
if the object to look for is null and this Collection doesn't support null elements.

public abstract boolean containsAll (Collection<?> collection)

Tests whether this Collection contains all objects contained in the specified Collection. If an element elem is contained several times in the specified Collection, the method returns true even if elem is contained only once in this Collection.

測試該collection是否包含特定collection中的所有物件,即使一個元素在特定的collection中出現多次,而此collection僅包含該元素一次也會返回true。
Parameters
collection the collection of objects.
Returns
  • true if all objects in the specified Collection are elements of this Collectionfalse otherwise.
Throws
if one or more elements of collection isn't of the correct type.
if collection contains at least one null element and this Collection doesn't support null elements.

public abstract boolean equals 

Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.

Parameters
object the object to compare with this object.
Returns
  • true if the object is the same as this object and false if it is different from this object.

public abstract int hashCode ()

Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.

Returns
  • the receiver's hash.

public abstract boolean isEmpty ()

Returns if this Collection contains no elements.

如果該collection沒有元素,則返回true,否則返回false。
Returns
  • true if this Collection has no elements, false otherwise.

public abstract Iterator<E> iterator ()

Returns an instance of  that may be used to access the objects contained by this Collection. The order in which the elements are returned by the iterator is not defined. Only if the instance of the Collection has a defined order the elements are returned in that order.

該方法返回一個iterator例項物件,用來訪問該collection中包含的物件。此iterator返回的順序是無序的,只有當collection中已經定義了元素的返回順序時,iterator才會按此順序返回元素。
iterator物件來自於collection介面繼承的iterable介面。
Returns
  • an iterator for accessing the Collection contents.

public abstract boolean remove 

Removes one instance of the specified object from this Collection if one is contained (optional). The element elem that is removed complies with (object==null ? elem==null : object.equals(elem).

如果該collection中包含需要移除的物件,那麼將移除該物件的一個例項(可能包含該物件的多個例項,將移除一個)。
Parameters
object the object to remove.
Returns
  • true if this Collection is modified, false otherwise.
Throws
if the object passed is not of the correct type.
if object is null and this Collection doesn't support null elements.

public abstract boolean removeAll (Collection<?> collection)

Removes all occurrences in this Collection of each object in the specified Collection (optional). After this method returns none of the elements in the passedCollection can be found in this Collection anymore.

移除特定collection中的所有物件在此collection中的存在。呼叫此方法後,特定collection中的元素不會在此collection中包含了。
Parameters
collection the collection of objects to remove.
Returns
  • true if this Collection is modified, false otherwise.
Throws
if one or more elements of collection isn't of the correct type.
if collection contains at least one null element and this Collection doesn't support null elements.

public abstract boolean retainAll (Collection<?> collection)

Removes all objects from this Collection that are not also found in the Collection passed (optional). After this method returns this Collection will only contain elements that also can be found in the Collection passed to this method.

移除該collection中所有給定collection中未包含的物件。呼叫此方法後,此collection中將只包含在給定collection中也存的的元素。
Parameters
collection the collection of objects to retain.
Returns
  • true if this Collection is modified, false otherwise.
Throws
if one or more elements of collection isn't of the correct type.
if collection contains at least one null element and this Collection doesn't support null elements.

public abstract int size ()

Returns a count of how many objects this Collection contains.

返回該collection中包含的物件的數量。如果此數量超過了Integer的最大值,將會返回Integer的最大值。
Returns
  • how many objects this Collection contains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in this Collection.

public abstract T[] toArray (T[] array)

Returns an array containing all elements contained in this Collection. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than this Collection, the array element following the Collection elements is set to null. If the implementation has ordered elements it will return the element array in the same order as an iterator would return them. toArray(new Object[0]) behaves exactly the same way as toArray() does.

該方法將會返回一個包含此collection中所有元素的陣列。如果給定的陣列足以裝下這些元素,就會使用給定的陣列,否則將會建立一個新的同類型的陣列。如果使用給定的陣列且陣列足以裝下此collection的元素,那麼陣列中除了collection中的元素,後面的元素將會被設定為null。如果collection的實現類是有序的,那麼陣列中將會按此順序儲存元素。toArrary(new object[0])和toArray()是一樣的。此方法不會對該collection造成影響。
Parameters
array the array.
Returns
  • an array of the elements from this Collection.
Throws
if the type of an element in this Collection cannot be stored in the type of the specified array.

public abstract Object[] toArray ()

Returns a new array containing all elements contained in this Collection. If the implementation has ordered elements it will return the element array in the same order as an iterator would return them. The array returned does not reflect any changes of the Collection. A new array is created even if the underlying data structure is already an array.

Returns
  • an array of the elements from this Collection.