java.util.Collections

來源:互聯網
上載者:User
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.