/** A collection of values, each an Object reference. */
publicinterfaceCollection<T>extendsIterable<T>{
/* Constructors. Classes that implement Collection should* have at least two constructors:* CLASS (): Constructs an empty CLASS* CLASS (C): Where C is any Collection, constructs a CLASS that* contains the same elements as C. *//* Required met hods: *//** The number of values in THIS. */
intsize();
/** True iff size () == 0. */
booleanisEmpty();
/** True iff THIS contains X: that is, if for some z in* THIS, either z and X are null, or z.equals (X). */
booleancontains(Objectx);
/** True iff contains(x) for all elements x in C. */
booleancontainsAll(Collection<?>c);
/** An iterator that yields all the elements of THIS, in some* order. */
Iterator<T>iterator();
/** A new array containing all elements of THIS. */
Object[]toArray();
/** Assuming ANARRAY has dynamic type T[] (where T is some* reference type), the result is an array of type T[] containing* all elements of THIS. The result is ANARRAY itself, if all of* these elements fit (leftover elements of ANARRAY are set to null).* Otherwise, the result is a new array. It is an error if not* all items in THIS are assignable to T. */
<T>T[]toArray(T[]anArray);
// Interface java.util.Collection, continued.
/* Optional methods. Any of these may do nothing except to* throw UnsupportedOperationException. */
/** Cause X to be contained in THIS. Returns true if the Collection *
/* changes as a result. */
booleanadd(Tx);
/** Cause all members of C to be contained in THIS. Returns true* if the object THIS changes as a result. */
booleanaddAll(Collection<?extendsT>c);
/** Remove all members of THIS. */
voidclear();
/** Remove a Object .equal to X from THIS, if one exists,* returning true iff the object THIS changes as a result. */
booleanremove(ObjectX);
/** Remove all elements, x, such that C.contains(x) (if any* are present), returning true iff there were any* objects removed. */
booleanremoveAll(Collection<?>c);
/** Intersection: Remove all elements, x, such that C.contains(x)* is false, returning true iff any items were removed. */
/** A Collection that contains at most one null item and in which no* two distinct non-null items are .equal. The effects of modifying* an item contained in a Set so as to change the value of .equal* on it are undefined. */
publicinterfaceSet<T>extendsCollection<T>{
/* Constructors. Classes that implement Set should* have at least two constructors:* CLASS (): Constructs an empty CLASS* CLASS (C): Where C is any Collection, constructs a CLASS that* contains the same elements as C, with duplicates removed. */
/** Cause X to be contained in THIS. Returns true iff X was *
/* not previously a member. */
booleanadd(Tx);
/** True iff S is a Set (instanceof Set) and is equal to THIS as a* set (size()==S.size() each of item in S is contained in THIS). */
booleanequals(ObjectS);
/** The sum of the values of x.hashCode () for all x in THIS, with* the hashCode of null taken to be 0. */
inthashCode();
/* Other methods inherited from Collection:* size, isEmpty, contains, containsAll, iterator, toArray,* addAll, clear, remove, removeAll, retainAll */
}
```
代码2.7 接口java.util.Set.只展示比Collection更具体的注释的方法。
``` java
packagejava.util;
/** An ordered sequence of items, indexed by numbers 0 .. N-1,* where N is the size() of the List. */
publicinterfaceList<T>extendsCollection<T>{
/* Required metho ds: *//** The Kth element of THIS, where 0 <= K < size(). Throws* IndexOutOfBoundsException if K is out of range. */
Tget(intk);
/** The first value k such that get(k) is null if X==null,* X.equals (get(k)), otherwise, or -1 if there is no such k. */
intindexOf(Objectx);
/** The largest value k such that get(k) is null if X==null,* X.equals (get(k)), otherwise, or -1 if there is no such k. */
intlastIndexOf(Objectx);
/* NOTE: The methods iterator, listIterator, and subList produce* views that become invalid if THIS is structurally modified by* any other means (see text). */
/** An iterator that yields all the elements of THIS, in proper* index order. (NOTE: it is always valid for iterator() to* return the same value as would listIterator, below.) */
Iterator<T>iterator();
/** A ListIterator that yields the elements K, K+1, ..., size()-1* of THIS, in that order, where 0 <= K <= size(). Throws* IndexOutOfBoundsException if K is out of range. */
ListIterator<T>listIterator(intk);
/** Same as listIterator (0) */
ListIterator<T>listIterator();
/** A view of THIS consisting of the elements L, L+1, ..., U-1,* in that order. Throws IndexOutOfBoundsException unless* 0 <= L <= U <= size(). */
List<T>subList(intL,intU);
/* Other methods inherited from Collection:* add, addAll, size, isEmpty, contains, containsAll, remove, toArray */