Learnitweb

What is Collections class in Java?

public class Collections extends Object

This class is a member of the Java Collections Framework. This class directly extends java.lang.Object and consists 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.

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

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Implementors are free to substitute other algorithms as long as the specification is adhered.

The algorithms of this class 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 UnsupportedOperationException exception if an invocation would have no effect on the collection. For example, invoking the Collections.sort() method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.

This class has 3 fields:

static ListEMPTY_LIST
The empty list (immutable).
static MapEMPTY_MAP
The empty map (immutable).
static SetEMPTY_SET
The empty set (immutable).

Some important methods of Collections class

static <T> booleanaddAll(Collection<? super T> c, T… elements)
Adds all of the specified elements to the specified collection.
static <T> voidcopy(List<? super T> dest, List<? extends T> src)
Copies all of the elements from one list to another.
static <T> List<T>emptyList()
Returns the empty list (immutable).
static <T> voidfill(List<? super T> list, T obj)
Replaces all of the elements of the specified list with the specified element.
static intfrequency(Collection<?> c, Object o)
Returns the number of elements in the specified collection equal to the specified object.
static void reverse(List<?> list)
Reverse the order of elements in the specified list.
static void shuffle(List<?> list)
Randomly permutes the specified list using a default source of randomness.
static <T extends Comparable<? super T>> voidsort(List<T> list)
Sorts the specified list into ascending order, according to the natural ordering of its elements.
static <T> List<T>synchronizedList(List<T> list)
Returns a synchronized(thread-safe) collection backed by the specified list
static <T> List<T>unmodifiabledList(List<? extends T> list)
Returns an unmodifiable view of the specified list.