Java. util. Collections

Source: Internet
Author: User
public class Collectionsextends Object
This class consists exclusively (only) of static methods that operate on or return collections. It contains polymorphic (polymorphism) algorithms (algorithm) that operate on collections, "wrappers", which return
A new collection backed by a specified collection, and a few other odds (possibility) and ends. This class is completely composed of static methods that operate on the collection or return the collection. It contains the polymorphism algorithm operated on the collection, that is, the package, which returns the new collection supported by the specified collection and a few other contents.

The methods of this class all throwNullpointerexceptionIf the collections or class objects provided to them are null.

If the collection or Class Object provided by the method of this class is null, these methods will throwNullpointerexception.

The documentation for the polymorphic algorithms contained in this class generally provided des a brief description ofImplementation. Such descriptions (description) shocould be regardedImplementation notes, Rather than parts ofSpecification.
Implementors shocould feel free to substitute (alternative) other algorithms, so long as the specification itself is adhered to (observed). (For example, the algorithm usedSortDoes not have to be a mergesort (merge sort), but it does have to beStable.)

The documentation for the polymorphism algorithms contained in this class usually includesImplementation. This type of description should be consideredImplementation ConsiderationsInsteadSpecifications. The implementers should be able to use other algorithms at will, as long as they follow the specifications themselves. (For example,SortThe algorithm used is not necessarily a merge sort algorithm, but it must beStable.)

The "destructive (corrupt)" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throwUnsupportedoperationexceptionIf the collection does not support
The appropriate mutation (variant) primitive (s), such asSetMethod. These algorithms may, but are not required to, throw this exception if an invocation wowould have no effect on the collection. For example, invokingSortMethod
On an unmodifiable list that is already sorted may or may not throwUnsupportedoperationexception.

The "destructive" algorithm contained in this class can be used to modify the collection algorithm it operates on. This algorithm is specified in collection and does not support appropriate variable elements (suchSetMethod ).Unsupportedoperationexception. If the call is not
If collection has any effect, these algorithms may (but are not required) throw this exception. For example, you can callSortMethod may (or may not) ThrowUnsupportedoperationexception.

This class is a member of the Java collections framework.

Since:

1.2
See also:

Collection,Set,List,Map

Ncopies

Public static <t> List <t> ncopies (int n, t o)
Returns an immutable (immutable) list consisting of n copies of the specified object. the newly allocated (allocated) data object is tiny (small) (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 code:

Package collection; import Java. util. collections; import Java. util. list; public class testcollections {public static void main (string [] ARGs) {// Java is generated. util. collections $ copieslist object // This private class inherits from javasactlist and does not implement the add method list = collections. ncopies (3, new copyobj (); system. out. println (list. getclass (). getname (); // list. add (New copyobj (); // report 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 (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 code:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.