java.util
Class Collections

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.Collections

public class Collections
extends Object sample code for java.lang.Object definition code for java.lang.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.

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. 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.)

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.

This class is a member of the Java Collections Framework.

Since:
1.2
See Also:
Collection sample code for java.util.Collection definition code for java.util.Collection , Set sample code for java.util.Set definition code for java.util.Set , List sample code for java.util.List definition code for java.util.List , Map sample code for java.util.Map definition code for java.util.Map

Field Summary
static List sample code for java.util.List definition code for java.util.List EMPTY_LIST sample code for java.util.Collections.EMPTY_LIST definition code for java.util.Collections.EMPTY_LIST
          The empty list (immutable).
static Map sample code for java.util.Map definition code for java.util.Map EMPTY_MAP sample code for java.util.Collections.EMPTY_MAP definition code for java.util.Collections.EMPTY_MAP
          The empty map (immutable).
static Set sample code for java.util.Set definition code for java.util.Set EMPTY_SET sample code for java.util.Collections.EMPTY_SET definition code for java.util.Collections.EMPTY_SET
          The empty set (immutable).
 
Method Summary
static
<T> boolean
addAll sample code for java.util.Collections.addAll(java.util.Collection, T...) definition code for java.util.Collections.addAll(java.util.Collection, T...) (Collection sample code for java.util.Collection definition code for java.util.Collection <? super T> c, T... a)
          Adds all of the specified elements to the specified collection.
static
<T> int
binarySearch sample code for java.util.Collections.binarySearch(java.util.List, T) definition code for java.util.Collections.binarySearch(java.util.List, T) (List sample code for java.util.List definition code for java.util.List <? extends Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <? super T>> list, T key)
          Searches the specified list for the specified object using the binary search algorithm.
static
<T> int
binarySearch sample code for java.util.Collections.binarySearch(java.util.List, T, java.util.Comparator) definition code for java.util.Collections.binarySearch(java.util.List, T, java.util.Comparator) (List sample code for java.util.List definition code for java.util.List <? extends T> list, T key, Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super T> c)
          Searches the specified list for the specified object using the binary search algorithm.
static
<E> Collection sample code for java.util.Collection definition code for java.util.Collection <E>
checkedCollection sample code for java.util.Collections.checkedCollection(java.util.Collection, java.lang.Class) definition code for java.util.Collections.checkedCollection(java.util.Collection, java.lang.Class) (Collection sample code for java.util.Collection definition code for java.util.Collection <E> c, Class sample code for java.lang.Class definition code for java.lang.Class <E> type)
          Returns a dynamically typesafe view of the specified collection.
static
<E> List sample code for java.util.List definition code for java.util.List <E>
checkedList sample code for java.util.Collections.checkedList(java.util.List, java.lang.Class) definition code for java.util.Collections.checkedList(java.util.List, java.lang.Class) (List sample code for java.util.List definition code for java.util.List <E> list, Class sample code for java.lang.Class definition code for java.lang.Class <E> type)
          Returns a dynamically typesafe view of the specified list.
static
<K,V> Map sample code for java.util.Map definition code for java.util.Map <K,V>
checkedMap sample code for java.util.Collections.checkedMap(java.util.Map, java.lang.Class, java.lang.Class) definition code for java.util.Collections.checkedMap(java.util.Map, java.lang.Class, java.lang.Class) (Map sample code for java.util.Map definition code for java.util.Map <K,V> m, Class sample code for java.lang.Class definition code for java.lang.Class <K> keyType, Class sample code for java.lang.Class definition code for java.lang.Class <V> valueType)
          Returns a dynamically typesafe view of the specified map.
static
<E> Set sample code for java.util.Set definition code for java.util.Set <E>
checkedSet sample code for java.util.Collections.checkedSet(java.util.Set, java.lang.Class) definition code for java.util.Collections.checkedSet(java.util.Set, java.lang.Class) (Set sample code for java.util.Set definition code for java.util.Set <E> s, Class sample code for java.lang.Class definition code for java.lang.Class <E> type)
          Returns a dynamically typesafe view of the specified set.
static
<K,V> SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
checkedSortedMap sample code for java.util.Collections.checkedSortedMap(java.util.SortedMap, java.lang.Class, java.lang.Class) definition code for java.util.Collections.checkedSortedMap(java.util.SortedMap, java.lang.Class, java.lang.Class) (SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> m, Class sample code for java.lang.Class definition code for java.lang.Class <K> keyType, Class sample code for java.lang.Class definition code for java.lang.Class <V> valueType)
          Returns a dynamically typesafe view of the specified sorted map.
static
<E> SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <E>
checkedSortedSet sample code for java.util.Collections.checkedSortedSet(java.util.SortedSet, java.lang.Class) definition code for java.util.Collections.checkedSortedSet(java.util.SortedSet, java.lang.Class) (SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <E> s, Class sample code for java.lang.Class definition code for java.lang.Class <E> type)
          Returns a dynamically typesafe view of the specified sorted set.
static
<T> void
copy sample code for java.util.Collections.copy(java.util.List, java.util.List) definition code for java.util.Collections.copy(java.util.List, java.util.List) (List sample code for java.util.List definition code for java.util.List <? super T> dest, List sample code for java.util.List definition code for java.util.List <? extends T> src)
          Copies all of the elements from one list into another.
static boolean disjoint sample code for java.util.Collections.disjoint(java.util.Collection, java.util.Collection) definition code for java.util.Collections.disjoint(java.util.Collection, java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <?> c1, Collection sample code for java.util.Collection definition code for java.util.Collection <?> c2)
          Returns true if the two specified collections have no elements in common.
static
<T> List sample code for java.util.List definition code for java.util.List <T>
emptyList sample code for java.util.Collections.emptyList() definition code for java.util.Collections.emptyList() ()
          Returns the empty list (immutable).
static
<K,V> Map sample code for java.util.Map definition code for java.util.Map <K,V>
emptyMap sample code for java.util.Collections.emptyMap() definition code for java.util.Collections.emptyMap() ()
          Returns the empty map (immutable).
static
<T> Set sample code for java.util.Set definition code for java.util.Set <T>
emptySet sample code for java.util.Collections.emptySet() definition code for java.util.Collections.emptySet() ()
          Returns the empty set (immutable).
static
<T> Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <T>
enumeration sample code for java.util.Collections.enumeration(java.util.Collection) definition code for java.util.Collections.enumeration(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <T> c)
          Returns an enumeration over the specified collection.
static
<T> void
fill sample code for java.util.Collections.fill(java.util.List, T) definition code for java.util.Collections.fill(java.util.List, T) (List sample code for java.util.List definition code for java.util.List <? super T> list, T obj)
          Replaces all of the elements of the specified list with the specified element.
static int frequency sample code for java.util.Collections.frequency(java.util.Collection, java.lang.Object) definition code for java.util.Collections.frequency(java.util.Collection, java.lang.Object) (Collection sample code for java.util.Collection definition code for java.util.Collection <?> c, Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Returns the number of elements in the specified collection equal to the specified object.
static int indexOfSubList sample code for java.util.Collections.indexOfSubList(java.util.List, java.util.List) definition code for java.util.Collections.indexOfSubList(java.util.List, java.util.List) (List sample code for java.util.List definition code for java.util.List <?> source, List sample code for java.util.List definition code for java.util.List <?> target)
          Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
static int lastIndexOfSubList sample code for java.util.Collections.lastIndexOfSubList(java.util.List, java.util.List) definition code for java.util.Collections.lastIndexOfSubList(java.util.List, java.util.List) (List sample code for java.util.List definition code for java.util.List <?> source, List sample code for java.util.List definition code for java.util.List <?> target)
          Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
static
<T> ArrayList sample code for java.util.ArrayList definition code for java.util.ArrayList <T>
list sample code for java.util.Collections.list(java.util.Enumeration) definition code for java.util.Collections.list(java.util.Enumeration) (Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <T> e)
          Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.
static
<T extends Object sample code for java.lang.Object definition code for java.lang.Object & Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <? super T>>
T
max sample code for java.util.Collections.max(java.util.Collection) definition code for java.util.Collections.max(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends T> coll)
          Returns the maximum element of the given collection, according to the natural ordering of its elements.
static
<T> T
max sample code for java.util.Collections.max(java.util.Collection, java.util.Comparator) definition code for java.util.Collections.max(java.util.Collection, java.util.Comparator) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends T> coll, Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super T> comp)
          Returns the maximum element of the given collection, according to the order induced by the specified comparator.
static
<T extends Object sample code for java.lang.Object definition code for java.lang.Object & Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <? super T>>
T
min sample code for java.util.Collections.min(java.util.Collection) definition code for java.util.Collections.min(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends T> coll)
          Returns the minimum element of the given collection, according to the natural ordering of its elements.
static
<T> T
min sample code for java.util.Collections.min(java.util.Collection, java.util.Comparator) definition code for java.util.Collections.min(java.util.Collection, java.util.Comparator) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends T> coll, Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super T> comp)
          Returns the minimum element of the given collection, according to the order induced by the specified comparator.
static
<T> List sample code for java.util.List definition code for java.util.List <T>
nCopies sample code for java.util.Collections.nCopies(int, T) definition code for java.util.Collections.nCopies(int, T) (int n, T o)
          Returns an immutable list consisting of n copies of the specified object.
static
<T> boolean
replaceAll sample code for java.util.Collections.replaceAll(java.util.List, T, T) definition code for java.util.Collections.replaceAll(java.util.List, T, T) (List sample code for java.util.List definition code for java.util.List <T> list, T oldVal, T newVal)
          Replaces all occurrences of one specified value in a list with another.
static void reverse sample code for java.util.Collections.reverse(java.util.List) definition code for java.util.Collections.reverse(java.util.List) (List sample code for java.util.List definition code for java.util.List <?> list)
          Reverses the order of the elements in the specified list.
static
<T> Comparator sample code for java.util.Comparator definition code for java.util.Comparator <T>
reverseOrder sample code for java.util.Collections.reverseOrder() definition code for java.util.Collections.reverseOrder() ()
          Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
static
<T> Comparator sample code for java.util.Comparator definition code for java.util.Comparator <T>
reverseOrder sample code for java.util.Collections.reverseOrder(java.util.Comparator) definition code for java.util.Collections.reverseOrder(java.util.Comparator) (Comparator sample code for java.util.Comparator definition code for java.util.Comparator <T> cmp)
          Returns a comparator that imposes the reverse ordering of the specified comparator.
static void rotate sample code for java.util.Collections.rotate(java.util.List, int) definition code for java.util.Collections.rotate(java.util.List, int) (List sample code for java.util.List definition code for java.util.List <?> list, int distance)
          Rotates the elements in the specified list by the specified distance.
static void shuffle sample code for java.util.Collections.shuffle(java.util.List) definition code for java.util.Collections.shuffle(java.util.List) (List sample code for java.util.List definition code for java.util.List <?> list)
          Randomly permutes the specified list using a default source of randomness.
static void shuffle sample code for java.util.Collections.shuffle(java.util.List, java.util.Random) definition code for java.util.Collections.shuffle(java.util.List, java.util.Random) (List sample code for java.util.List definition code for java.util.List <?> list, Random sample code for java.util.Random definition code for java.util.Random  rnd)
          Randomly permute the specified list using the specified source of randomness.
static
<T> Set sample code for java.util.Set definition code for java.util.Set <T>
singleton sample code for java.util.Collections.singleton(T) definition code for java.util.Collections.singleton(T) (T o)
          Returns an immutable set containing only the specified object.
static
<T> List sample code for java.util.List definition code for java.util.List <T>
singletonList sample code for java.util.Collections.singletonList(T) definition code for java.util.Collections.singletonList(T) (T o)
          Returns an immutable list containing only the specified object.
static
<K,V> Map sample code for java.util.Map definition code for java.util.Map <K,V>
singletonMap sample code for java.util.Collections.singletonMap(K, V) definition code for java.util.Collections.singletonMap(K, V) (K key, V value)
          Returns an immutable map, mapping only the specified key to the specified value.
static
<T extends Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <? super T>>
void
sort sample code for java.util.Collections.sort(java.util.List) definition code for java.util.Collections.sort(java.util.List) (List sample code for java.util.List definition code for java.util.List <T> list)
          Sorts the specified list into ascending order, according to the natural ordering of its elements.
static
<T> void
sort sample code for java.util.Collections.sort(java.util.List, java.util.Comparator) definition code for java.util.Collections.sort(java.util.List, java.util.Comparator) (List sample code for java.util.List definition code for java.util.List <T> list, Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super T> c)
          Sorts the specified list according to the order induced by the specified comparator.
static void swap sample code for java.util.Collections.swap(java.util.List, int, int) definition code for java.util.Collections.swap(java.util.List, int, int) (List sample code for java.util.List definition code for java.util.List <?> list, int i, int j)
          Swaps the elements at the specified positions in the specified list.
static
<T> Collection sample code for java.util.Collection definition code for java.util.Collection <T>
synchronizedCollection sample code for java.util.Collections.synchronizedCollection(java.util.Collection) definition code for java.util.Collections.synchronizedCollection(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <T> c)
          Returns a synchronized (thread-safe) collection backed by the specified collection.
static
<T> List sample code for java.util.List definition code for java.util.List <T>
synchronizedList sample code for java.util.Collections.synchronizedList(java.util.List) definition code for java.util.Collections.synchronizedList(java.util.List) (List sample code for java.util.List definition code for java.util.List <T> list)
          Returns a synchronized (thread-safe) list backed by the specified list.
static
<K,V> Map sample code for java.util.Map definition code for java.util.Map <K,V>
synchronizedMap sample code for java.util.Collections.synchronizedMap(java.util.Map) definition code for java.util.Collections.synchronizedMap(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <K,V> m)
          Returns a synchronized (thread-safe) map backed by the specified map.
static
<T> Set sample code for java.util.Set definition code for java.util.Set <T>
synchronizedSet sample code for java.util.Collections.synchronizedSet(java.util.Set) definition code for java.util.Collections.synchronizedSet(java.util.Set) (Set sample code for java.util.Set definition code for java.util.Set <T> s)
          Returns a synchronized (thread-safe) set backed by the specified set.
static
<K,V> SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
synchronizedSortedMap sample code for java.util.Collections.synchronizedSortedMap(java.util.SortedMap) definition code for java.util.Collections.synchronizedSortedMap(java.util.SortedMap) (SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> m)
          Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.
static
<T> SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <T>
synchronizedSortedSet sample code for java.util.Collections.synchronizedSortedSet(java.util.SortedSet) definition code for java.util.Collections.synchronizedSortedSet(java.util.SortedSet) (SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <T> s)
          Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.
static
<T> Collection sample code for java.util.Collection definition code for java.util.Collection <T>
unmodifiableCollection sample code for java.util.Collections.unmodifiableCollection(java.util.Collection) definition code for java.util.Collections.unmodifiableCollection(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends T> c)
          Returns an unmodifiable view of the specified collection.
static
<T> List sample code for java.util.List definition code for java.util.List <T>
unmodifiableList sample code for java.util.Collections.unmodifiableList(java.util.List) definition code for java.util.Collections.unmodifiableList(java.util.List) (List sample code for java.util.List definition code for java.util.List <? extends T> list)
          Returns an unmodifiable view of the specified list.
static
<K,V> Map sample code for java.util.Map definition code for java.util.Map <K,V>
unmodifiableMap sample code for java.util.Collections.unmodifiableMap(java.util.Map) definition code for java.util.Collections.unmodifiableMap(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
          Returns an unmodifiable view of the specified map.
static
<T> Set sample code for java.util.Set definition code for java.util.Set <T>
unmodifiableSet sample code for java.util.Collections.unmodifiableSet(java.util.Set) definition code for java.util.Collections.unmodifiableSet(java.util.Set) (Set sample code for java.util.Set definition code for java.util.Set <? extends T> s)
          Returns an unmodifiable view of the specified set.
static
<K,V> SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
unmodifiableSortedMap sample code for java.util.Collections.unmodifiableSortedMap(java.util.SortedMap) definition code for java.util.Collections.unmodifiableSortedMap(java.util.SortedMap) (SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,? extends V> m)
          Returns an unmodifiable view of the specified sorted map.
static
<T> SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <T>
unmodifiableSortedSet sample code for java.util.Collections.unmodifiableSortedSet(java.util.SortedSet) definition code for java.util.Collections.unmodifiableSortedSet(java.util.SortedSet) (SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <T> s)
          Returns an unmodifiable view of the specified sorted set.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() ,