Java util collection_java.util.Collection

java.util.Collection

collection hierarchy的最顶层接口,collection是一组object的集合,jdk不提供任何直接实现Collection的类,但是对Collection的子接口(比如set、list)提供了实现类。这个接口的典型用法是遍历集合、大部分集合操作(包括包含关系判断、集合的并、差操作;集合的交操作不是基本操作,没有在Collection中定义)。

/*** The root interface in the collection hierarchy. A collection

* represents a group of objects, known as its elements. Some

* collections allow duplicate elements and others do not. Some are ordered

* and others unordered. The JDK does not provide any direct

* implementations of this interface: it provides implementations of more

* specific subinterfaces like Set and List. This interface

* is typically used to pass collections around and manipulate them where

* maximum generality is desired.

*@param the type of elements in this collection

*

*@authorJosh Bloch

*@authorNeal Gafter

*@seeSet

*@seeList

*@seeMap

*@seeSortedSet

*@seeSortedMap

*@seeHashSet

*@seeTreeSet

*@seeArrayList

*@seeLinkedList

*@seeVector

*@seeCollections

*@seeArrays

*@seeAbstractCollection

*@since1.2*/

public interface Collection extends Iterable {

Collection定义的几个方法

Object[] toArray();这个方法是数组和collection之间的桥梁。

返回一个包含了集合内全部元素的数组。如果iterator方法返回的元素有序,那么此方法必须返回相同的顺序。

函数返回的数组是新开辟的,collection不包含返回数组的任何引用,这意味着可以任意修改返回数组而不影响collection

/*** Returns an array containing all of the elements in this collection.

* If this collection makes any guarantees as to what order its elements

* are returned by its iterator, this method must return the elements in

* the same order.

*

*

The returned array will be "safe" in that no references to it are

* maintained by this collection. (In other words, this method must

* allocate a new array even if this collection is backed by an array).

* The caller is thus free to modify the returned array.

*

*

This method acts as bridge between array-based and collection-based

* APIs.

*

*@returnan array containing all of the elements in this collection*/Object[] toArray();

T[] toArray(T[] a);类似于Object[] toArray();但是有区别。

返回指定类型的数组,包含collection的全部元素,如果collection正好可以放进参数数组里,那么返回这个参数数组的引用。否则,返回一个新开辟的数组,大小和collection相同。

如果参数数组长度比collection大,那么后面的位置都赋值为null。

如果iterator方法返回有序,那么此方法返回同样顺序。

第四段是重点。此方法也是array和collection之间的桥梁。  T[] toArray(T[] a)比Object[] toArray()更深一层的作用是:可以再运行时动态指定返回数组的类型,在某些情况下,这可以减少开辟数组的花销。哪些情况下呢?

/*** Returns an array containing all of the elements in this collection;

* the runtime type of the returned array is that of the specified array.

* If the collection fits in the specified array, it is returned therein.

* Otherwise, a new array is allocated with the runtime type of the

* specified array and the size of this collection.

*

*

If this collection fits in the specified array with room to spare

* (i.e., the array has more elements than this collection), the element

* in the array immediately following the end of the collection is set to

* null. (This is useful in determining the length of this

* collection only if the caller knows that this collection does

* not contain any null elements.)

*

*

If this collection makes any guarantees as to what order its elements

* are returned by its iterator, this method must return the elements in

* the same order.

*

*

Like the {@link#toArray()} method, this method acts as bridge between

* array-based and collection-based APIs. Further, this method allows

* precise control over the runtime type of the output array, and may,

* under certain circumstances, be used to save allocation costs.

*

*

Suppose x is a collection known to contain only strings.

* The following code can be used to dump the collection into a newly

* allocated array of String:

*

*

 
 

* String[] y = x.toArray(new String[0]);

*

* Note that toArray(new Object[0]) is identical in function to

* toArray().

*

*@parama the array into which the elements of this collection are to be

* stored, if it is big enough; otherwise, a new array of the same

* runtime type is allocated for this purpose.

*@returnan array containing all of the elements in this collection

*@throwsArrayStoreException if the runtime type of the specified array

* is not a supertype of the runtime type of every element in

* this collection

*@throwsNullPointerException if the specified array is null*/

T[] toArray(T[] a);

判断集合包含关系的函数boolean containsAll(Collection> c);

注意:返回true不一定说明this和c包含相同的元素,只能说明this包含c。

集合的并操作 boolean addAll(Collection extends E> c);

此操作不包括以下情况:addAll的过程中参数被修改了;(一个例子就是,把非空集合本身做参数,因为addAll的过程中this一定会被修改)

/*** Adds all of the elements in the specified collection to this collection

* (optional operation). The behavior of this operation is undefined if

* the specified collection is modified while the operation is in progress.

* (This implies that the behavior of this call is undefined if the

* specified collection is this collection, and this collection is

* nonempty.)

boolean addAll(Collection extends E> c);

集合的差操作boolean removeAll(Collection> c);

Collection定义了一个Object的同名方法boolean equals(Object o);Collection接口本身不对equals作任何规定,但其子接口可能有特定的想法。

/*** Compares the specified object with this collection for equality.

*

* While the Collection interface adds no stipulations to the

* general contract for the Object.equals, programmers who

* implement the Collection interface "directly" (in other words,

* create a class that is a Collection but is not a Set

* or a List) must exercise care if they choose to override the

* Object.equals. It is not necessary to do so, and the simplest

* course of action is to rely on Object's implementation, but

* the implementor may wish to implement a "value comparison" in place of

* the default "reference comparison." (The List and

* Set interfaces mandate such value comparisons.)

*

* The general contract for the Object.equals method states that

* equals must be symmetric (in other words, a.equals(b) if and

* only if b.equals(a)). The contracts for List.equals

* and Set.equals state that lists are only equal to other lists,

* and sets to other sets. Thus, a custom equals method for a

* collection class that implements neither the List nor

* Set interface must return false when this collection

* is compared to any list or set. (By the same logic, it is not possible

* to write a class that correctly implements both the Set and

* List interfaces.)

*

*@paramo object to be compared for equality with this collection

*@returntrue if the specified object is equal to this

* collection

*

*@seeObject#equals(Object)

*@seeSet#equals(Object)

*@seeList#equals(Object)*/

boolean equals(Object o);

哈希函数同理,子接口的实现可能有特定的需求

int hashCode();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值