Final

java.util
Class Collections

java.lang.Object
  extended by java.util.Collections

public class Collections
extends Object

Collections contains static methods which operate on Collection classes.

Since:
1.2

Field Summary
static List EMPTY_LIST
          The unmutable, serializable empty list.
static Map EMPTY_MAP
          The unmutable, serializable empty map.
static Set EMPTY_SET
          The unmutable, serializable empty set.
 
Method Summary
static
<T> int
binarySearch(List<? extends Comparable<? super T>> list, T object)
          Performs a binary search for the specified element in the specified sorted List.
static int binarySearch(List list, Object object, Comparator comparator)
          Performs a binary search for the specified element in the specified sorted List using the specified Comparator.
static void copy(List destination, List source)
          Copies the elements from the source list to the destination list.
static
<T> List<T>
emptyList()
          Returns an unmutable, serializable empty list.
static
<K,V> Map<K,V>
emptyMap()
          Returns an unmutable, serializable empty map.
static
<T> Set<T>
emptySet()
          Returns an unmutable, serializable empty set.
static Enumeration enumeration(Collection collection)
          Answers an Enumeration on the specified Collection.
static void fill(List list, Object object)
          Fills the specified List with the specified element.
static int indexOfSubList(List list, List sublist)
          Searches the list for sublist and answers the beginning index of the first occurance.
static int lastIndexOfSubList(List list, List sublist)
          Searches the list for sublist and answers the beginning index of the last occurance.
static ArrayList list(Enumeration enumeration)
          Answers an ArrayList with all the elements in the enumeration.
static Object max(Collection collection)
          Searches the specified Collection for the maximum element.
static Object max(Collection collection, Comparator comparator)
          Searches the specified Collection for the maximum element using the specified Comparator.
static Object min(Collection collection)
          Searches the specified Collection for the minimum element.
static Object min(Collection collection, Comparator comparator)
          Searches the specified Collection for the minimum element using the specified Comparator.
static List nCopies(int length, Object object)
          Answers a List containing the specified number of the specified element.
static boolean replaceAll(List list, Object obj, Object obj2)
          Replaces all occurances of Object obj in list with newObj.
static void reverse(List list)
          Reverses the order of the elements in the specified List.
static Comparator reverseOrder()
          A Comparator which reverses the natural order of the elements.
static void rotate(List list, int dist)
          Rotates the elements in List list by the distancedist
static void shuffle(List list)
          Moves every element of the List to a random new position in the list.
static void shuffle(List list, Random random)
          Moves every element of the List to a random new position in the list using the specified random number generator.
static Set singleton(Object object)
          Answers a Set containing the specified element.
static List singletonList(Object object)
          Answers a List containing the specified element.
static Map singletonMap(Object key, Object value)
          Answers a Map containing the specified key and value.
static void sort(List list)
          Sorts the specified List in ascending order.
static void sort(List list, Comparator comparator)
          Sorts the specified List using the specified Comparator.
static void swap(List list, int index1, int index2)
          Swaps the elements of List list at indices index1 and index2
static
<T> Collection<T>
synchronizedCollection(Collection<T> collection)
          Answers a wrapper on the specified Collection which synchronizes all access to the Collection.
static
<T> List<T>
synchronizedList(List<T> list)
          Answers a wrapper on the specified List which synchronizes all access to the List.
static
<K,V> Map<K,V>
synchronizedMap(Map<K,V> map)
          Answers a wrapper on the specified Map which synchronizes all access to the Map.
static
<T> Set<T>
synchronizedSet(Set<T> set)
          Answers a wrapper on the specified Set which synchronizes all access to the Set.
static
<K,V> SortedMap<K,V>
synchronizedSortedMap(SortedMap<K,V> map)
          Answers a wrapper on the specified SortedMap which synchronizes all access to the SortedMap.
static
<T> SortedSet<T>
synchronizedSortedSet(SortedSet<T> set)
          Answers a wrapper on the specified SortedSet which synchronizes all access to the SortedSet.
static
<T> Collection<T>
unmodifiableCollection(Collection<? extends T> collection)
          Answers a wrapper on the specified Collection which throws an UnsupportedOperationException whenever an attempt is made to modify the Collection.
static
<T> List<T>
unmodifiableList(List<? extends T> list)
          Answers a wrapper on the specified List which throws an UnsupportedOperationException whenever an attempt is made to modify the List.
static
<K,V> Map<K,V>
unmodifiableMap(Map<? extends K,? extends V> map)
          Answers a wrapper on the specified Map which throws an UnsupportedOperationException whenever an attempt is made to modify the Map.
static
<T> Set<T>
unmodifiableSet(Set<? extends T> set)
          Answers a wrapper on the specified Set which throws an UnsupportedOperationException whenever an attempt is made to modify the Set.
static
<K,V> SortedMap<K,V>
unmodifiableSortedMap(SortedMap<K,? extends V> map)
          Answers a wrapper on the specified SortedMap which throws an UnsupportedOperationException whenever an attempt is made to modify the SortedMap.
static
<T> SortedSet<T>
unmodifiableSortedSet(SortedSet<T> set)
          Answers a wrapper on the specified SortedSet which throws an UnsupportedOperationException whenever an attempt is made to modify the SortedSet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_LIST

public static final List EMPTY_LIST
The unmutable, serializable empty list.

See Also:
emptyList()

EMPTY_SET

public static final Set EMPTY_SET
The unmutable, serializable empty set.

See Also:
emptySet()

EMPTY_MAP

public static final Map EMPTY_MAP
The unmutable, serializable empty map.

Since:
1.3
See Also:
emptyMap()
Method Detail

emptyList

@SuppressWarnings(value="unchecked")
public static final <T> List<T> emptyList()
Returns an unmutable, serializable empty list. Unlike the direct access to EMPTY_LIST this method provides type safety.

Type Parameters:
T - the types theoretically contained in this list
Returns:
the empty list
Since:
1.5

emptyMap

@SuppressWarnings(value="unchecked")
public static final <K,V> Map<K,V> emptyMap()
Returns an unmutable, serializable empty map. Unlike the direct access to EMPTY_MAP this method provides type safety.

Type Parameters:
K - the key types theoretically contained in this map
V - the value types theoretically contained in this map
Returns:
the empty map
Since:
1.5

emptySet

@SuppressWarnings(value="unchecked")
public static final <T> Set<T> emptySet()
Returns an unmutable, serializable empty set. Unlike the direct access to EMPTY_SET this method provides type safety.

Type Parameters:
T - the types theoretically contained in this set
Returns:
the empty set
Since:
1.5

binarySearch

public static <T> int binarySearch(List<? extends Comparable<? super T>> list,
                                   T object)
Performs a binary search for the specified element in the specified sorted List.

Parameters:
list - the sorted List to search
object - the element to find
Returns:
the non-negative index of the element, or a negative index which is the -index - 1 where the element would be inserted
Throws:
ClassCastException - when an element in the List or the seach element does not implement Comparable, or cannot be compared to each other

binarySearch

public static int binarySearch(List list,
                               Object object,
                               Comparator comparator)
Performs a binary search for the specified element in the specified sorted List using the specified Comparator.

Parameters:
list - the sorted List to search
object - the element to find
comparator - the Comparator or null if Comparable.compareTo(Object) should be used
Returns:
the non-negative index of the element, or a negative index which is the -index - 1 where the element would be inserted
Throws:
ClassCastException - when an element in the list and the searched element cannot be compared to each other using the Comparator

copy

public static void copy(List destination,
                        List source)
Copies the elements from the source list to the destination list.

Parameters:
destination - the copy-to list
source - the copy-from list
Throws:
IndexOutOfBoundsException - when the destination List is smaller than the source List
UnsupportedOperationException - when replacing an element in the destination list is not supported

enumeration

public static Enumeration enumeration(Collection collection)
Answers an Enumeration on the specified Collection.

Parameters:
collection - the Collection to enumerate
Returns:
an Enumeration

fill

public static void fill(List list,
                        Object object)
Fills the specified List with the specified element.

Parameters:
list - the List to fill
object - the fill element
Throws:
UnsupportedOperationException - when replacing an element in the List is not supported

max

public static Object max(Collection collection)
Searches the specified Collection for the maximum element.

Parameters:
collection - the Collection to search
Returns:
the maximum element in the Collection
Throws:
ClassCastException - when an element in the Collection does not implement Comparable or elements cannot be compared to each other

max

public static Object max(Collection collection,
                         Comparator comparator)
Searches the specified Collection for the maximum element using the specified Comparator.

Parameters:
collection - the Collection to search
comparator - the Comparator
Returns:
the maximum element in the Collection
Throws:
ClassCastException - when elements in the Collection cannot be compared to each other using the Comparator

min

public static Object min(Collection collection)
Searches the specified Collection for the minimum element.

Parameters:
collection - the Collection to search
Returns:
the minimum element in the Collection
Throws:
ClassCastException - when an element in the Collection does not implement Comparable or elements cannot be compared to each other

min

public static Object min(Collection collection,
                         Comparator comparator)
Searches the specified Collection for the minimum element using the specified Comparator.

Parameters:
collection - the Collection to search
comparator - the Comparator
Returns:
the minimum element in the Collection
Throws:
ClassCastException - when elements in the Collection cannot be compared to each other using the Comparator

nCopies

public static List nCopies(int length,
                           Object object)
Answers a List containing the specified number of the specified element. The list cannot be modified.

Parameters:
length - the size of the returned List
object - the element
Returns:
a List containing length copies of the element
Throws:
IllegalArgumentException - when length < 0

reverse

public static void reverse(List list)
Reverses the order of the elements in the specified List.

Parameters:
list - the List to reverse
Throws:
UnsupportedOperationException - when replacing an element in the List is not supported

reverseOrder

public static Comparator reverseOrder()
A Comparator which reverses the natural order of the elements.

Returns:
a Comparator

shuffle

public static void shuffle(List list)
Moves every element of the List to a random new position in the list.

Parameters:
list - the List to shuffle
Throws:
UnsupportedOperationException - when replacing an element in the List is not supported

shuffle

public static void shuffle(List list,
                           Random random)
Moves every element of the List to a random new position in the list using the specified random number generator.

Parameters:
list - the List to shuffle
random - the random number generator
Throws:
UnsupportedOperationException - when replacing an element in the List is not supported

singleton

public static Set singleton(Object object)
Answers a Set containing the specified element. The set cannot be modified.

Parameters:
object - the element
Returns:
a Set containing the element

singletonList

public static List singletonList(Object object)
Answers a List containing the specified element. The list cannot be modified.

Parameters:
object - the element
Returns:
a List containing the element

singletonMap

public static Map singletonMap(Object key,
                               Object value)
Answers a Map containing the specified key and value. The map cannot be modified.

Parameters:
key - the key
value - the value
Returns:
a Map containing the key and value

sort

public static void sort(List list)
Sorts the specified List in ascending order.

Parameters:
list - the List to be sorted
Throws:
ClassCastException - when an element in the List does not implement Comparable or elements cannot be compared to each other

sort

public static void sort(List list,
                        Comparator comparator)
Sorts the specified List using the specified Comparator.

Parameters:
list - the List to be sorted
comparator - the Comparator
Throws:
ClassCastException - when elements in the List cannot be compared to each other using the Comparator

swap

public static void swap(List list,
                        int index1,
                        int index2)
Swaps the elements of List list at indices index1 and index2

Parameters:
list - the List to manipulate on
index1 - int position of the first element to swap with the element in index2
index2 - int position of the other element
Throws:
IndexOutOfBoundsException - if index1 or index2 is out of range of this list

replaceAll

public static boolean replaceAll(List list,
                                 Object obj,
                                 Object obj2)
Replaces all occurances of Object obj in list with newObj. If the obj is null, then all occurances of null is replaced with newObj.

Parameters:
list - the List to modify
obj - the Object to find and replace occurances of.
obj2 - the Object to replace all occurances of obj in list
Returns:
true, if at least one occurance of obj has been found in list
Throws:
UnsupportedOperationException - if the list does not support setting elements

rotate

public static void rotate(List list,
                          int dist)
Rotates the elements in List list by the distancedist

e.g. for a given list with elements [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], calling rotate(list, 3) or rotate(list, -7) would modify the list to look like this: [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]

Parameters:
list -
dist - It can be any integer: 0, positive, negative, larger than the list size

indexOfSubList

public static int indexOfSubList(List list,
                                 List sublist)
Searches the list for sublist and answers the beginning index of the first occurance.

-1 is returned if the sublist does not exist in list

Parameters:
list - the List to search sublist in
sublist - the List to search in list
Returns:
the beginning index of the first occurance of sublist in list, or -1

lastIndexOfSubList

public static int lastIndexOfSubList(List list,
                                     List sublist)
Searches the list for sublist and answers the beginning index of the last occurance.

-1 is returned if the sublist does not exist in list

Parameters:
list - the List to search sublist in
sublist - the List to search in list
Returns:
the beginning index of the last occurance of sublist in list, or -1

list

public static ArrayList list(Enumeration enumeration)
Answers an ArrayList with all the elements in the enumeration. The elements in the returned ArrayList are in the same order as in the enumeration.

Parameters:
enumeration - Enumeration
Returns:
and ArrayList

synchronizedCollection

public static <T> Collection<T> synchronizedCollection(Collection<T> collection)
Answers a wrapper on the specified Collection which synchronizes all access to the Collection.

Parameters:
collection - the Collection
Returns:
a synchronized Collection
Throws:
NullPointerException - if collection is null

synchronizedList

public static <T> List<T> synchronizedList(List<T> list)
Answers a wrapper on the specified List which synchronizes all access to the List.

Parameters:
list - the List
Returns:
a synchronized List
Throws:
NullPointerException - if list is null

synchronizedMap

public static <K,V> Map<K,V> synchronizedMap(Map<K,V> map)
Answers a wrapper on the specified Map which synchronizes all access to the Map.

Parameters:
map - the Map
Returns:
a synchronized Map
Throws:
NullPointerException - if map is null

synchronizedSet

public static <T> Set<T> synchronizedSet(Set<T> set)
Answers a wrapper on the specified Set which synchronizes all access to the Set.

Parameters:
set - the Set
Returns:
a synchronized Set
Throws:
NullPointerException - if set is null

synchronizedSortedMap

public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> map)
Answers a wrapper on the specified SortedMap which synchronizes all access to the SortedMap.

Parameters:
map - the SortedMap
Returns:
a synchronized SortedMap
Throws:
NullPointerException - if map is null

synchronizedSortedSet

public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> set)
Answers a wrapper on the specified SortedSet which synchronizes all access to the SortedSet.

Parameters:
set - the SortedSet
Returns:
a synchronized SortedSet
Throws:
NullPointerException - if set is null

unmodifiableCollection

public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> collection)
Answers a wrapper on the specified Collection which throws an UnsupportedOperationException whenever an attempt is made to modify the Collection.

Parameters:
collection - the Collection
Returns:
an unmodifiable Collection
Throws:
NullPointerException - if collection is null

unmodifiableList

public static <T> List<T> unmodifiableList(List<? extends T> list)
Answers a wrapper on the specified List which throws an UnsupportedOperationException whenever an attempt is made to modify the List.

Parameters:
list - the List
Returns:
an unmodifiable List
Throws:
NullPointerException - if list is null

unmodifiableMap

public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> map)
Answers a wrapper on the specified Map which throws an UnsupportedOperationException whenever an attempt is made to modify the Map.

Parameters:
map - the Map
Returns:
a unmodifiable Map
Throws:
NullPointerException - if map is null

unmodifiableSet

public static <T> Set<T> unmodifiableSet(Set<? extends T> set)
Answers a wrapper on the specified Set which throws an UnsupportedOperationException whenever an attempt is made to modify the Set.

Parameters:
set - the Set
Returns:
a unmodifiable Set
Throws:
NullPointerException - if set is null

unmodifiableSortedMap

public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> map)
Answers a wrapper on the specified SortedMap which throws an UnsupportedOperationException whenever an attempt is made to modify the SortedMap.

Parameters:
map - the SortedMap
Returns:
a unmodifiable SortedMap
Throws:
NullPointerException - if map is null

unmodifiableSortedSet

public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> set)
Answers a wrapper on the specified SortedSet which throws an UnsupportedOperationException whenever an attempt is made to modify the SortedSet.

Parameters:
set - the SortedSet
Returns:
a unmodifiable SortedSet
Throws:
NullPointerException - if set is null

Final

Licensed Materials - Property of IBM
© Copyright IBM Corp. 2006, 2008 All Rights Reserved.