java.util
Class Collections

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

EMPTY_MAP

sort

Sorts the specified list into ascending order, according to the
natural ordering of its elements. All elements in the list must
implement the Comparable interface. Furthermore, all elements
in the list must be mutually comparable (that is,
e1.compareTo(e2) must not throw a ClassCastException
for any elements e1 and e2 in the list).

This sort is guaranteed to be stable: equal elements will
not be reordered as a result of the sort.

The specified list must be modifiable, but need not be resizable.

The sorting algorithm is a modified mergesort (in which the merge is
omitted if the highest element in the low sublist is less than the
lowest element in the high sublist). This algorithm offers guaranteed
n log(n) performance, and can approach linear performance on nearly
sorted lists.

This implementation dumps the specified list into an array, sorts
the array, and iterates over the list resetting each element
from the corresponding position in the array. This avoids the
n2 log(n) performance that would result from attempting
to sort a linked list in place.

Parameters:

list - the list to be sorted.

Throws:

ClassCastException - if the list contains elements that are not
mutually comparable (for example, strings and integers).

sort

Sorts the specified list according to the order induced by the
specified comparator. All elements in the list must be mutually
comparable using the specified comparator (that is,
c.compare(e1, e2) must not throw a ClassCastException
for any elements e1 and e2 in the list).

This sort is guaranteed to be stable: equal elements will
not be reordered as a result of the sort.

The sorting algorithm is a modified mergesort (in which the merge is
omitted if the highest element in the low sublist is less than the
lowest element in the high sublist). This algorithm offers guaranteed
n log(n) performance, and can approach linear performance on nearly
sorted lists.

The specified list must be modifiable, but need not be resizable.
This implementation dumps the specified list into an array, sorts
the array, and iterates over the list resetting each element
from the corresponding position in the array. This avoids the
n2 log(n) performance that would result from attempting
to sort a linked list in place.

Parameters:

list - the list to be sorted.

c - the comparator to determine the order of the list. A
null value indicates that the elements' natural
ordering should be used.

Throws:

ClassCastException - if the list contains elements that are not
mutually comparable using the specified comparator.

binarySearch

Searches the specified list for the specified object using the binary
search algorithm. The list must be sorted into ascending order
according to the natural ordering of its elements (as by the
sort(List) method, above) prior to making this call. If it is
not sorted, the results are undefined. If the list contains multiple
elements equal to the specified object, there is no guarantee which one
will be found.

This method runs in log(n) time for a "random access" list (which
provides near-constant-time positional access). It may
run in n log(n) time if it is called on a "sequential access" list
(which provides linear-time positional access).

If the specified list implements the AbstracSequentialList
interface, this method will do a sequential search instead of a binary
search; this offers linear performance instead of n log(n) performance
if this method is called on a LinkedList object.

Parameters:

list - the list to be searched.

key - the key to be searched for.

Returns:

index of the search key, if it is contained in the list;
otherwise, (-(insertion point) - 1). The
insertion point is defined as the point at which the
key would be inserted into the list: the index of the first
element greater than the key, or list.size(), if all
elements in the list are less than the specified key. Note
that this guarantees that the return value will be >= 0 if
and only if the key is found.

Throws:

ClassCastException - if the list contains elements that are not
mutually comparable (for example, strings and
integers), or the search key in not mutually comparable
with the elements of the list.

binarySearch

Searches the specified list for the specified object using the binary
search algorithm. The list must be sorted into ascending order
according to the specified comparator (as by the Sort(List,
Comparator) method, above), prior to making this call. If it is
not sorted, the results are undefined. If the list contains multiple
elements equal to the specified object, there is no guarantee which one
will be found.

This method runs in log(n) time for a "random access" list (which
provides near-constant-time positional access). It may
run in n log(n) time if it is called on a "sequential access" list
(which provides linear-time positional access).

If the specified list implements the AbstracSequentialList
interface, this method will do a sequential search instead of a binary
search; this offers linear performance instead of n log(n) performance
if this method is called on a LinkedList object.

Parameters:

list - the list to be searched.

key - the key to be searched for.

c - the comparator by which the list is ordered. A
null value indicates that the elements' natural
ordering should be used.

Returns:

index of the search key, if it is contained in the list;
otherwise, (-(insertion point) - 1). The
insertion point is defined as the point at which the
key would be inserted into the list: the index of the first
element greater than the key, or list.size(), if all
elements in the list are less than the specified key. Note
that this guarantees that the return value will be >= 0 if
and only if the key is found.

Throws:

ClassCastException - if the list contains elements that are not
mutually comparable using the specified comparator,
or the search key in not mutually comparable with the
elements of the list using this comparator.

shuffle

Randomly permutes the specified list using a default source of
randomness. All permutations occur with approximately equal
likelihood.

The hedge "approximately" is used in the foregoing description because
default source of randomenss is only approximately an unbiased source
of independently chosen bits. If it were a perfect source of randomly
chosen bits, then the algorithm would choose permutations with perfect
uniformity.

This implementation traverses the list backwards, from the last element
up to the second, repeatedly swapping a randomly selected element into
the "current position". Elements are randomly selected from the
portion of the list that runs from the first element to the current
position, inclusive.

This method runs in linear time for a "random access" list (which
provides near-constant-time positional access). It may require
quadratic time for a "sequential access" list.

shuffle

Randomly permute the specified list using the specified source of
randomness. All permutations occur with equal likelihood
assuming that the source of randomness is fair.

This implementation traverses the list backwards, from the last element
up to the second, repeatedly swapping a randomly selected element into
the "current position". Elements are randomly selected from the
portion of the list that runs from the first element to the current
position, inclusive.

This method runs in linear time for a "random access" list (which
provides near-constant-time positional access). It may require
quadratic time for a "sequential access" list.

copy

Copies all of the elements from one list into another. After the
operation, the index of each copied element in the destination list
will be identical to its index in the source list. The destination
list must be at least as long as the source list. If it is longer, the
remaining elements in the destination list are unaffected.

min

Returns the minimum element of the given collection, according to the
natural ordering of its elements. All elements in the
collection must implement the Comparable interface.
Furthermore, all elements in the collection must be mutually
comparable (that is, e1.compareTo(e2) must not throw a
ClassCastException for any elements e1 and
e2 in the collection).

This method iterates over the entire collection, hence it requires
time proportional to the size of the collection.

Parameters:

coll - the collection whose minimum element is to be determined.

Returns:

the minimum element of the given collection, according
to the natural ordering of its elements.

Throws:

ClassCastException - if the collection contains elements that are
not mutually comparable (for example, strings and
integers).

min

Returns the minimum element of the given collection, according to the
order induced by the specified comparator. All elements in the
collection must be mutually comparable by the specified
comparator (that is, comp.compare(e1, e2) must not throw a
ClassCastException for any elements e1 and
e2 in the collection).

This method iterates over the entire collection, hence it requires
time proportional to the size of the collection.

Parameters:

coll - the collection whose minimum element is to be determined.

comp - the comparator with which to determine the minimum element.
A null value indicates that the elements' natural
ordering should be used.

Returns:

the minimum element of the given collection, according
to the specified comparator.

Throws:

ClassCastException - if the collection contains elements that are
not mutually comparable using the specified comparator.

max

Returns the maximum element of the given collection, according to the
natural ordering of its elements. All elements in the
collection must implement the Comparable interface.
Furthermore, all elements in the collection must be mutually
comparable (that is, e1.compareTo(e2) must not throw a
ClassCastException for any elements e1 and
e2 in the collection).

This method iterates over the entire collection, hence it requires
time proportional to the size of the collection.

Parameters:

coll - the collection whose maximum element is to be determined.

Returns:

the maximum element of the given collection, according
to the natural ordering of its elements.

Throws:

ClassCastException - if the collection contains elements that are
not mutually comparable (for example, strings and
integers).

max

Returns the maximum element of the given collection, according to the
order induced by the specified comparator. All elements in the
collection must be mutually comparable by the specified
comparator (that is, comp.compare(e1, e2) must not throw a
ClassCastException for any elements e1 and
e2 in the collection).

This method iterates over the entire collection, hence it requires
time proportional to the size of the collection.

Parameters:

coll - the collection whose maximum element is to be determined.

comp - the comparator with which to determine the maximum element.
A null value indicates that the elements' natural
ordering should be used.

Returns:

the maximum element of the given collection, according
to the specified comparator.

Throws:

ClassCastException - if the collection contains elements that are
not mutually comparable using the specified comparator.

unmodifiableCollection

Returns an unmodifiable view of the specified collection. This method
allows modules to provide users with "read-only" access to internal
collections. Query operations on the returned collection "read through"
to the specified collection, and attempts to modify the returned
collection, whether direct or via its iterator, result in an
UnsupportedOperationException.

The returned collection does not pass the hashCode and equals
operations through to the backing collection, but relies on
Object's equals and hashCode methods. This
is necessary to preserve the contracts of these operations in the case
that the backing collection is a set or a list.

The returned collection will be serializable if the specified collection
is serializable.

Parameters:

c - the collection for which an unmodifiable view is to be
returned.

Returns:

an unmodifiable view of the specified collection.

unmodifiableSet

Returns an unmodifiable view of the specified set. This method allows
modules to provide users with "read-only" access to internal sets.
Query operations on the returned set "read through" to the specified
set, and attempts to modify the returned set, whether direct or via its
iterator, result in an UnsupportedOperationException.

The returned set will be serializable if the specified set
is serializable.

Parameters:

s - the set for which an unmodifiable view is to be returned.

Returns:

an unmodifiable view of the specified set.

unmodifiableSortedSet

Returns an unmodifiable view of the specified sorted set. This method
allows modules to provide users with "read-only" access to internal
sorted sets. Query operations on the returned sorted set "read
through" to the specified sorted set. Attempts to modify the returned
sorted set, whether direct, via its iterator, or via its
subSet, headSet, or tailSet views, result in
an UnsupportedOperationException.

The returned sorted set will be serializable if the specified sorted set
is serializable.

Parameters:

s - the sorted set for which an unmodifiable view is to be
returned.

Returns:

an unmodifiable view of the specified sorted set.

unmodifiableList

Returns an unmodifiable view of the specified list. This method allows
modules to provide users with "read-only" access to internal
lists. Query operations on the returned list "read through" to the
specified list, and attempts to modify the returned list, whether
direct or via its iterator, result in an
UnsupportedOperationException.

The returned list will be serializable if the specified list
is serializable.

Parameters:

list - the list for which an unmodifiable view is to be returned.

Returns:

an unmodifiable view of the specified list.

unmodifiableMap

Returns an unmodifiable view of the specified map. This method
allows modules to provide users with "read-only" access to internal
maps. Query operations on the returned map "read through"
to the specified map, and attempts to modify the returned
map, whether direct or via its collection views, result in an
UnsupportedOperationException.

The returned map will be serializable if the specified map
is serializable.

Parameters:

m - the map for which an unmodifiable view is to be returned.

Returns:

an unmodifiable view of the specified map.

unmodifiableSortedMap

Returns an unmodifiable view of the specified sorted map. This method
allows modules to provide users with "read-only" access to internal
sorted maps. Query operations on the returned sorted map "read through"
to the specified sorted map. Attempts to modify the returned
sorted map, whether direct, via its collection views, or via its
subMap, headMap, or tailMap views, result in
an UnsupportedOperationException.

The returned sorted map will be serializable if the specified sorted map
is serializable.

Parameters:

m - the sorted map for which an unmodifiable view is to be
returned.

Returns:

an unmodifiable view of the specified sorted map.

synchronizedCollection

Returns a synchronized (thread-safe) collection backed by the specified
collection. In order to guarantee serial access, it is critical that
all access to the backing collection is accomplished
through the returned collection.

It is imperative that the user manually synchronize on the returned
collection when iterating over it:

Failure to follow this advice may result in non-deterministic behavior.

The returned collection does not pass the hashCode
and equals operations through to the backing collection, but
relies on Object's equals and hashCode methods. This is
necessary to preserve the contracts of these operations in the case
that the backing collection is a set or a list.

The returned collection will be serializable if the specified collection
is serializable.

synchronizedSortedSet

Returns a synchronized (thread-safe) sorted set backed by the specified
sorted set. In order to guarantee serial access, it is critical that
all access to the backing sorted set is accomplished
through the returned sorted set (or its views).

It is imperative that the user manually synchronize on the returned
sorted set when iterating over it or any of its subSet,
headSet, or tailSet views.

synchronizedList

Returns a synchronized (thread-safe) list backed by the specified
list. In order to guarantee serial access, it is critical that
all access to the backing list is accomplished
through the returned list.

It is imperative that the user manually synchronize on the returned
list when iterating over it:

synchronizedSortedMap

Returns a synchronized (thread-safe) sorted map backed by the specified
sorted map. In order to guarantee serial access, it is critical that
all access to the backing sorted map is accomplished
through the returned sorted map (or its views).

It is imperative that the user manually synchronize on the returned
sorted map when iterating over any of its collection views, or the
collections views of any of its subMap, headMap or
tailMap views.

nCopies

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.

reverseOrder

Returns a comparator that imposes the reverse of the natural
ordering on a collection of objects that implement the
Comparable interface. (The natural ordering is the ordering
imposed by the objects' own compareTo method.) This enables a
simple idiom for sorting (or maintaining) collections (or arrays) of
objects that implement the Comparable interface in
reverse-natural-order. For example, suppose a is an array of
strings. Then:

Arrays.sort(a, Collections.reverseOrder());

sorts the array in reverse-lexicographic (alphabetical) order.

The returned comparator is serializable.

Returns:

a comparator that imposes the reverse of the natural
ordering on a collection of objects that implement
the Comparable interface.