java.util.Collections

来源:互联网 发布:淘宝样式重置 编辑:程序博客网 时间:2024/05/01 11:55
public class Collectionsextends Object
This class consists exclusively(仅仅) of static methods that operate on or return collections. It contains polymorphic(多态) algorithms(算法) that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds(可能性) and ends.
此类完全由在 collection 上进行操作或返回 collection 的静态方法组成。它包含在 collection 上操作的多态算法,即“包装器”,包装器返回由指定 collection 支持的新 collection,以及少数其他内容。

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

如果为此类的方法所提供的 collection 或类对象为 null,则这些方法都将抛出 NullPointerException

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions(说明) should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute(替代) other algorithms, so long as the specification itself is adhered to(遵守). (For example, the algorithm used by sort does not have to be a mergesort(归并排序), but it does have to be stable.)

此类中所含多态算法的文档通常包括对实现 的简短描述。应该将这类描述视为实现注意事项,而不是规范 的一部分。实现者应该可以随意使用其他算法替代,只要遵循规范本身即可。(例如,sort 使用的算法不一定是合并排序算法,但它必须是稳定的。)

The "destructive(破坏的)" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation(变种) primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.

此类中包含的“破坏性”算法,即可修改其所操作的 collection 的算法,该算法被指定在 collection 不支持适当的可变基元(比如 set 方法)时抛出UnsupportedOperationException。如果调用不会对 collection 产生任何影响,那么这些算法可能(但不要求)抛出此异常。例如,在已经排序的、不可修改列表上调用 sort 方法可能会(也可能不会)抛出 UnsupportedOperationException

This class is a member of the Java Collections Framework.

Since:

1.2
See Also:

CollectionSetListMap




nCopies

public static <T> List<T> nCopies(int n,T o)
Returns an immutable(不可变) list consisting of n copies of the specified object. The newly allocated(分配) data object is tiny(小) (it contains a single reference to the data object). This method is useful in combination with the List.addAll method to grow lists. The returned list is serializable.

Parameters:

n - the number of elements in the returned list.
o - the element to appear repeatedly in the returned list.

Returns:

an immutable list consisting of n copies of the specified object.

Throws:

IllegalArgumentException - if n < 0

See Also:

List.addAll(Collection), List.addAll(int, Collection)

java代码:

package Collection;import java.util.Collections;import java.util.List;public class TestCollections {public static void main(String[] args) {//生成的是java.util.Collections$CopiesList对象//这个私有类继承自AbstractList,没有实现add方法List list = Collections.nCopies(3, new CopyObj());System.out.println(list.getClass().getName());//list.add(new CopyObj());//报UnsupportedOperationExceptionfor(int i = 0;i < list.size(); i++){System.out.print(list.get(i) + " ");}}}class CopyObj{}


fill

public static <T> void fill(List<? super T> list,T obj)
Replaces all of the elements of the specified list with the specified element.
This method runs in linear(线性) time.

Parameters:

list - the list to be filled with the specified element.
obj - The element with which to fill the specified list.

Throws:

UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.

java代码:

package Collection;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class TestCollectionsFill {public static void main(String[] args) {List list = new ArrayList();list.add("1");list.add("2");list.add("3");Collections.fill(list, new CopyObj());System.out.println(list.getClass().getName());list.add(new CopyObj());for(int i = 0;i < list.size(); i++){System.out.print(list.get(i) + " ");}}}



11

原创粉丝点击