Java数据结构-Collection(一)
来源:互联网 发布:淘宝店铺去哪里推广 编辑:程序博客网 时间:2024/05/20 15:12
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 Collection
s. 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 true
, false
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
Returns
true
if thisCollection
is modified,false
otherwise.
Throws
Collection
is not supported.Collection
.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
of objects.Returns
true
if thisCollection
is modified,false
otherwise.
Throws
Collection
is not supported.Collection
.Collection
.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
Collection
is not supported.See Also
isEmpty()
size()
public abstract boolean contains (Object object)
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
Returns
true
if object is an element of thisCollection
,false
otherwise.
Throws
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
Returns
true
if all objects in the specifiedCollection
are elements of thisCollection
,false
otherwise.
Throws
collection
isn't of the correct type.collection
contains at least one null
element and this Collection
doesn't support null
elements.collection
is null
.public abstract boolean equals (Object object)
Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
Returns
true
if the object is the same as this object andfalse
if it is different from this object.
See Also
hashCode()
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.
See Also
equals(Object)
public abstract boolean isEmpty ()
Returns if this Collection
contains no elements.
如果该collection没有元素,则返回true,否则返回false。
Returns
true
if thisCollection
has no elements,false
otherwise.
See Also
size()
public abstract Iterator<E> iterator ()
Returns an instance of Iterator
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 (Object object)
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
Returns
true
if thisCollection
is modified,false
otherwise.
Throws
Collection
is not supported.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
Returns
true
if thisCollection
is modified,false
otherwise.
Throws
Collection
is not supported.collection
isn't of the correct type.collection
contains at least one null
element and this Collection
doesn't support null
elements.collection
is null
.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
Returns
true
if thisCollection
is modified,false
otherwise.
Throws
Collection
is not supported.collection
isn't of the correct type.collection
contains at least one null
element and this Collection
doesn't support null
elements.collection
is null
.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 thisCollection
.
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
Returns
- an array of the elements from this
Collection
.
Throws
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
.
- Java数据结构-Collection(一)
- Java数据结构详解(一)-Collection接口
- 数据结构整理(一)Collection与Map
- java(14):数据结构(1)--Collection
- Java数据结构(一)——StringBuffer,StringBuilder,Collection,Iterator,List
- 用java源代码学数据结构<一>: Collection , Iterator
- 集合详解(一):Collection以及简单数据结构
- Java Collection 接口描述(一)
- JAVA第十五弹《Collection(一)》
- java 7 collection 详解(一)
- Java集合详解(一):Collection
- java容器 (一) Collection简介
- java集合框架(一)Collection
- java容器(一) Collection
- Java--容器(Collection)(一)
- java中的数据结构 Collection & Map
- java数据结构(一)
- Java Collection框架分析一
- vmware提示:此虚拟机似乎正在使用中,无法取得所有权的解决办法
- hduoj 5223 GCD 构造检查
- 文本分类
- try-catch-finally中return的执行情况
- FIFO & LRU
- Java数据结构-Collection(一)
- https://leetcode.com/problems/two-sum/
- Rails中文Podcasts【一】
- Yet another end of the world
- mysql group_concat() 使用
- lambda与proc的区别
- hdu1026 Ignatius and the Princess I(BFS+优先队列)
- 杂记(与美国佬的科技差距)
- 文章标题