java.util.AbstractCollection翻译

来源:互联网 发布:2016人口老龄化数据 编辑:程序博客网 时间:2024/05/18 03:57
 Overview Package  Class Use Tree Deprecated Index Help JavaTM 2 Platform
Std. Ed. v1.4.2
 PREV CLASS   NEXT CLASSFRAMES    NO FRAMES     All Classes SUMMARY: NESTED | FIELD | CONSTR | METHODDETAIL: FIELD | CONSTR | METHOD

java.util
Class AbstractCollection

java.lang.Object  extended byjava.util.AbstractCollection
All Implemented Interfaces:
Collection
Direct Known Subclasses:
AbstractList, AbstractSet

public abstract class AbstractCollection
extends Object
implements Collection

This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface. 该类提供了接口Collection的框架实现,用来降低实现该接口的复杂程度。

To implement an unmodifiable collection, the programmer needs only to extend this class and provide implementations for the iterator and size methods. (The iterator returned by the iterator method must implement hasNext and next.) 为了实现一个不可修改的集合,程序员只需要继承该类,提供iterator和size方法的实现即可。 (iterator方法返回的迭代器必须实现hasNext和next。)

To implement a modifiable collection, the programmer must additionally override this class's add method (which otherwise throws an UnsupportedOperationException), and the iterator returned by the iterator method must additionally implement its remove method. 为了实现一个可修改的集合,程序员必须另外重写该类的add方法(否则总是抛出 UnsupportedOperationException),iterator方法返回的迭代器必须实现remove方法。

The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification. 根据Collection接口文档的推荐,程序员通常提供一个空(不含参数)的Collection构造函数。

The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the collection being implemented admits a more efficient implementation. 文档对该类的每一个非抽象方法描述了实现细节。这些方法中的每一个都可以被重写 如果集合采取了更有效的实现方式。

This class is a member of the Java Collections Framework. 该接口是Java集合框架成员之一。

Since:
1.2
See Also:
Collection

Constructor Summaryprotected AbstractCollection()
          Sole constructor. 唯一的构造函数。
  Method Summary booleanadd(Object o)
          Ensures that this collection contains the specified element (optional operation). 确保集合包含指定元素(可选操作)。
 booleanaddAll(Collection c)
          Adds all of the elements in the specified collection to this collection (optional operation). 向集合添加指定集合的所有元素(可选操作)。
 voidclear()
          Removes all of the elements from this collection (optional operation). 从集合中删除所有元素(可选操作)。
 booleancontains(Object o)
          Returns true if this collection contains the specified element. 如果集合包含指定元素,返回true。
 booleancontainsAll(Collection c)
          Returns true if this collection contains all of the elements in the specified collection. 如果集合包含指定集合的所有元素,返回true。
 booleanisEmpty()
          Returns true if this collection contains no elements. 如果集合不含元素,返回true。
abstract  Iteratoriterator()
          Returns an iterator over the elements contained in this collection. 返回集合元素的迭代器。
 booleanremove(Object o)
          Removes a single instance of the specified element from this collection, if it is present (optional operation). 删除集合中指定元素的一个实例,若有的话(可选操作)。
 booleanremoveAll(Collection c)
          Removes from this collection all of its elements that are contained in the specified collection (optional operation). 从当前集合中删除指定集合所含的所有元素(可选操作)。
 booleanretainAll(Collection c)
          Retains only the elements in this collection that are contained in the specified collection (optional operation). 只在当前集合中保留指定集合所含的所有元素(可选操作)。
abstract  intsize()
          Returns the number of elements in this collection. 返回集合的元素数目。
 Object[]toArray()
          Returns an array containing all of the elements in this collection. 返回包含集合所有元素的数组。
 Object[]toArray(Object[] a)
          Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. 返回包含集合所有元素的数组,返回数组的运行时类型由指定数组的运行时类型决定。
 StringtoString()
          Returns a string representation of this collection. 返回集合的字符串形式。
  Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait  Methods inherited from interface java.util.Collectionequals, hashCode 

Constructor Detail

AbstractCollection

protected AbstractCollection()
Sole constructor. (For invocation by subclass constructors, typically implicit.) 唯一的构造函数。(通常由子类构造函数隐式调用)

Method Detail

iterator

public abstract Iterator iterator()
Returns an iterator over the elements contained in this collection. 返回集合元素的迭代器。

Specified by:
iterator in interface Collection
Returns:
an iterator over the elements contained in this collection. 集合元素的迭代器。

size

public abstract int size()
Returns the number of elements in this collection. If the collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE. 返回集合中元素的数量。如果集合包含的数量超过Integer.MAX_VALUE,返回Integer.MAX_VALUE。

Specified by:
size in interface Collection
Returns:
the number of elements in this collection. 集合的元素数目。

isEmpty

public boolean isEmpty()
Returns true if this collection contains no elements. 如果集合不含元素,返回true。

This implementation returns size() == 0. 实现:返回size() == 0。

Specified by:
isEmpty in interface Collection
Returns:
true if this collection contains no elements. 如果集合不含元素,返回true。

contains

public boolean contains(Object o)
Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)). 如果集合包含指定元素,返回true。正式表述为,只有当集合包含至少一个元素e, 使得 (o==null ? e==null : o.equals(e)),返回true。

This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element. 实现:对集合的元素进行遍历,依次检查每个元素是否和指定元素相等。

Specified by:
contains in interface Collection
Parameters:
o - object to be checked for containment in this collection. 用来测试集合中是否包含的对象。
Returns:
true if this collection contains the specified element. 如果集合包含指定元素,返回true。

toArray

public Object[] toArray()
Returns an array containing all of the elements in this collection. If the 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 the collection. (In other words, this method must allocate a new array even if the collection is backed by an Array). The caller is thus free to modify the returned array. 返回包含集合所有元素的数组。如果集合保证它所含元素的顺序为迭代器的顺序, 那么方法必须以相同顺序返回元素。返回的数组是安全的,集合中不会维持对其的引用。 (换句话说,即使集合是由数组得到的,方法也必须创建一个新的数组。) 调用者因此可以对返回数组进行修改。

This implementation allocates the array to be returned, and iterates over the elements in the collection, storing each object reference in the next consecutive element of the array, starting with element 0. 实现:创建用于返回的数组,遍历集合中的元素,从元素0开始,向数组连续地 储存每一个对象的引用。

Specified by:
toArray in interface Collection
Returns:
an array containing all of the elements in this collection. 包含集合所有元素的数组。

toArray

public Object[] toArray(Object[] a)
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 the collection fits in the specified array with room to spare (i.e., the array has more elements than the 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 the collection only if the caller knows that the collection does not contain any null elements.) 如果当前集合填入指定数组,空间上还有剩余(也就是数组的元素比集合多), 那么数组中紧随集合后的元素都被设为null。只有当调用者知道集合不包含null元素时, 确定集合的长度才是有用的。

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. 如果集合保证它所含元素的顺序为迭代器的顺序,那么方法必须以相同顺序返回元素。

This implementation checks if the array is large enough to contain the collection; if not, it allocates a new array of the correct size and type (using reflection). Then, it iterates over the collection, storing each object reference in the next consecutive element of the array, starting with element 0. If the array is larger than the collection, a null is stored in the first location after the end of the collection. 实现:检查数据是否足够容纳集合;如果不,使用合适的大小和类型新建一个数组(采用反射)。 然后,遍历集合,从元素0开始,向数组连续地储存每一个对象的引用。如果数组大于集合, 从集合结束的位置储存null。

Specified by:
toArray in interface Collection
Parameters:
a - the array into which the elements of the collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. 存储集合元素的数组,如果它足够大的话;否则为此创建一个运行时类型相同的新数组。
Returns:
an array containing the elements of the collection. 包含集合元素的数组。
Throws:
NullPointerException - if the specified array is null. 如果指定数组为null时抛出。
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this collection. 如果指定数组的运行时类型不是集合中每个元素运行时类型的父一级类型时抛出。

add

public boolean add(Object o)
Ensures that this collection contains the specified element (optional operation). Returns true if the collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.) Collections that support this operation may place limitations on what elements may be added to the collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added. 确保集合包含指定元素(可选操作)。如果集合因此而改变,返回true。 (如果集合以及包含了指定元素而不允许重复,返回false。) 集合支持该操作对加入集合的元素进行限制。特别地,某些集合拒绝加入null元素, 其它一些对加入元素的类型强加限制。集合类应该在文档中明确指出对加入元素的限制

This implementation always throws an UnsupportedOperationException. 实现:总是抛出UnsupportedOperationException。

Specified by:
add in interface Collection
Parameters:
o - element whose presence in this collection is to be ensured. 确保集合中存在的元素。
Returns:
true if the collection changed as a result of the call. 如果集合因调用而改变,返回true。
Throws:
UnsupportedOperationException - if the add method is not supported by this collection. 如果集合不支持add方法时抛出。
NullPointerException - if this collection does not permit null elements, and the specified element is null. 如果集合不允许null元素而指定元素为null时抛出。
ClassCastException - if the class of the specified element prevents it from being added to this collection. 如果指定元素的类型阻止其加入集合时抛出。
IllegalArgumentException - if some aspect of this element prevents it from being added to this collection. 如果元素的某些方面不允许它加入当前集合则抛出。

remove

public boolean remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the collection contains one or more such elements. Returns true if the collection contained the specified element (or equivalently, if the collection changed as a result of the call). 删除集合中指定元素的一个实例,若有的话(可选操作)。正式表述为, 如果集合包含一个或多个元素e,使得(o==null ? e==null : o.equals(e)),那么删除 一个元素e。如果集合包含指定元素(等价于集合因调用而改变),返回true。

This implementation iterates over the collection looking for the specified element. If it finds the element, it removes the element from the collection using the iterator's remove method. 实现:对集合进行遍历,查找指定元素。如果找到该元素,使用iterator的 remove方法从集合中删除。

Note that this implementation throws an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection contains the specified object. 注意如果集合返回的迭代器没有实现remove方法而且集合包含了指定元素, 实现将抛出UnsupportedOperationException。

Specified by:
remove in interface Collection
Parameters:
o - element to be removed from this collection, if present. 要从集合中删除的元素,若有。
Returns:
true if the collection contained the specified element. 如果集合包含指定元素,返回true。
Throws:
UnsupportedOperationException - if the remove method is not supported by this collection. 如果集合不支持remove方法时抛出。

containsAll

public boolean containsAll(Collection c)
Returns true if this collection contains all of the elements in the specified collection. 如果集合包含指定集合的所有元素,返回true。

This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. If all elements are so contained true is returned, otherwise false. 实现:对指定集合进行遍历,依次检查由迭代器返回的每一个元素,查看当前集合中是否包含。 如果所有元素都包含,返回true,否则为false。

Specified by:
containsAll in interface Collection
Parameters:
c - collection to be checked for containment in this collection. 检查当前集合是否包含的集合。
Returns:
true if this collection contains all of the elements in the specified collection. 如果集合包含指定集合的所有元素,返回true。
Throws:
NullPointerException - if the specified collection is null. 如果指定集合为null时抛出。
See Also:
contains(Object)

addAll

public boolean addAll(Collection c)
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.) 向集合添加指定集合的所有元素(可选操作)。如果在该操作过程中指定集合被修改, 操作行为无法确定。 (这意味着如果指定集合为当前集合,当前集合不为空,那么 调用的行为无法确定。)

This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn. 实现:对指定集合进行遍历,依次向当前集合添加每一个由迭代器返回的对象。

Note that this implementation will throw an UnsupportedOperationException unless add is overridden (assuming the specified collection is non-empty). 注意实现将抛出UnsupportedOperationException除非add被重写(假定指定集合非空)。

Specified by:
addAll in interface Collection
Parameters:
c - collection whose elements are to be added to this collection. 要被加入当前集合的元素。
Returns:
true if this collection changed as a result of the call. 如果集合因调用而改变,返回true。
Throws:
UnsupportedOperationException - if this collection does not support the addAll method. 如果集合不支持addAll方法时抛出。
NullPointerException - if the specified collection is null. 如果指定集合为null时抛出。
See Also:
add(Object)

removeAll

public boolean removeAll(Collection c)
Removes from this collection all of its elements that are contained in the specified collection (optional operation). 从当前集合中删除指定集合所含的所有元素(可选操作)。

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's so contained, it's removed from this collection with the iterator's remove method. 实现:对当前集合进行遍历,依次检查由迭代器返回的每一个元素,查看指定集合中是否包含。 如果包含,使用iterator的remove方法从当前集合中删除。

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more elements in common with the specified collection. 注意如果集合返回的迭代器没有实现remove方法而且集合包含了指定集合中 存在的一个或多个元素,实现将抛出UnsupportedOperationException。

Specified by:
removeAll in interface Collection
Parameters:
c - elements to be removed from this collection. 要从集合中删除的元素。
Returns:
true if this collection changed as a result of the call. 如果集合因调用而改变,返回true。
Throws:
UnsupportedOperationException - if the removeAll method is not supported by this collection. 如果集合不支持removeAll方法时抛出。
NullPointerException - if the specified collection is null. 如果指定集合为null时抛出。
See Also:
remove(Object), contains(Object)

retainAll

public boolean retainAll(Collection c)
Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection. 只在当前集合中保留指定集合所含的所有元素(可选操作)。换句话说,从当前集合中删除 指定集合中不含的所有元素。

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's not so contained, it's removed from this collection with the iterator's remove method. 实现:对当前集合进行遍历,依次检查由迭代器返回的每一个元素,查看指定集合中是否包含。 如果不包含,使用iterator的remove方法从当前集合中删除。

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more elements not present in the specified collection. 注意如果集合返回的迭代器没有实现remove方法而且集合包含了指定集合中 不存在的一个或多个元素,实现将抛出UnsupportedOperationException。

Specified by:
retainAll in interface Collection
Parameters:
c - elements to be retained in this collection. 集合中要保留的元素。
Returns:
true if this collection changed as a result of the call. 如果集合因调用而改变,返回true。
Throws:
UnsupportedOperationException - if the retainAll method is not supported by this Collection. 如果集合不支持retainAll方法时抛出。
NullPointerException - if the specified collection is null. 如果指定集合为null时抛出。
See Also:
remove(Object), contains(Object)

clear

public void clear()
Removes all of the elements from this collection (optional operation). The collection will be empty after this call returns (unless it throws an exception). 从集合中删除所有元素(可选操作)。方法返回后集合将为空除非抛出异常。

This implementation iterates over this collection, removing each element using the Iterator.remove operation. Most implementations will probably choose to override this method for efficiency. 实现:对当前集合进行遍历,使用Iterator.remove操作删除每一个元素。 大多数的实现因为效率考虑有可能选择来重写该方法。

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection is non-empty. 注意如果集合返回的迭代器没有实现remove方法而且集合不为空实现将抛出UnsupportedOperationException。

Specified by:
clear in interface Collection
Throws:
UnsupportedOperationException - if the clear method is not supported by this collection. 如果集合不支持clear方法时抛出。

toString

public String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object). 返回集合的字符串形式。字符串形式由一列以中括号“[]”括起的集合元素组成,顺序为迭代器返回的顺序。 相邻元素以字符“, ”(逗号和空格)分隔。元素由String.valueOf(Object)转换成字符串。

This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned. 实现:创建一个空stringBuffer,添加左中括号,遍历集合,依次添加每一个元素的字符串形式。 每一个元素后,添加“, ”,最后添加右中括号。由stringBuffer获取字符串,返回。

Overrides:
toString in class Object
Returns:
a string representation of this collection. 集合的字符串形式。

Overview Package  Class Use Tree Deprecated Index Help JavaTM 2 Platform
Std. Ed. v1.4.2
 PREV CLASS   NEXT CLASSFRAMES    NO FRAMES     All Classes SUMMARY: NESTED | FIELD | CONSTR | METHODDETAIL: FIELD | CONSTR | METHOD
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright 2003 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

原创粉丝点击