Top Description Inners Fields Constructors Methods
java.util

public Class Collections

extends Object
Class Inheritance
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.lang.reflect.Array, java.util.function.BiConsumer, .BiFunction, .Consumer, .Function, .IntFunction, .Predicate, .UnaryOperator, java.util.random.RandomGenerator, java.util.stream.IntStream, .Stream, .StreamSupport, jdk.internal.access.SharedSecrets

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.

Authors
Josh Bloch, Neal Gafter
Since
1.2
See Also
Collection, Set, List, Map

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class
pack-priv static class
private static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
private static class
pack-priv static class
pack-priv static class
private static class
pack-priv static class
Collections.SynchronizedNavigableMap<K, V>

A synchronized NavigableMap.

pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
private static class
pack-priv static class
pack-priv static class
Collections.UnmodifiableNavigableSet<
type of elements
E
>

Wraps a navigable set and disables all of the mutative operations.

pack-priv static class
pack-priv static class
private static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
public static final List<E>
EMPTY_LIST

The empty list (immutable).

public static final Map<K, V>
EMPTY_MAP

The empty map (immutable).

public static final Set<E>
EMPTY_SET

The empty set (immutable).

private static final int
private static final int
private static Random
r

private static final int
private static final int
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <
the class of the elements to add and of the collection
T
>
boolean

Returns:

true if the collection changed as a result of the call
addAll
(Collection<? super T>
the collection into which elements are to be inserted
c
,
T...
the elements to insert into c
elements
)

Adds all of the specified elements to the specified collection.

public static <
the class of the objects in the deque
T
>
Queue<T>

Returns:

the queue
asLifoQueue
(Deque<T>
the deque
deque
)

Returns a view of a Deque as a Last-in-first-out (Lifo) Queue.

public static <
the class of the objects in the list
T
>
int

Returns:

the 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.
binarySearch
(List<? extends Comparable<? super T>>
the list to be searched.
list
,
T
the key to be searched for.
key
)

Searches the specified list for the specified object using the binary search algorithm.

public static <
the class of the objects in the list
T
>
int

Returns:

the 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.
binarySearch
(List<? extends T>
the list to be searched.
list
,
T
the key to be searched for.
key
,
Comparator<? super T>
the comparator by which the list is ordered. A null value indicates that the elements' natural ordering should be used.
c
)

Searches the specified list for the specified object using the binary search algorithm.

public static <
the class of the objects in the collection
E
>
Collection<E>

Returns:

a dynamically typesafe view of the specified collection
checkedCollection
(Collection<E>
the collection for which a dynamically typesafe view is to be returned
c
,
Class<E>
the type of element that c is permitted to hold
type
)

Returns a dynamically typesafe view of the specified collection.

public static <
the class of the objects in the list
E
>
List<E>

Returns:

a dynamically typesafe view of the specified list
checkedList
(List<E>
the list for which a dynamically typesafe view is to be returned
list
,
Class<E>
the type of element that list is permitted to hold
type
)

Returns a dynamically typesafe view of the specified list.

public static <
the class of the map keys
K
,
the class of the map values
V
>
Map<K, V>

Returns:

a dynamically typesafe view of the specified map
checkedMap
(Map<K, V>
the map for which a dynamically typesafe view is to be returned
m
,
Class<K>
the type of key that m is permitted to hold
keyType
,
Class<V>
the type of value that m is permitted to hold
valueType
)

Returns a dynamically typesafe view of the specified map.

public static <
type of map keys
K
,
type of map values
V
>
NavigableMap<K, V>

Returns:

a dynamically typesafe view of the specified map
checkedNavigableMap
(NavigableMap<K, V>
the map for which a dynamically typesafe view is to be returned
m
,
Class<K>
the type of key that m is permitted to hold
keyType
,
Class<V>
the type of value that m is permitted to hold
valueType
)

Returns a dynamically typesafe view of the specified navigable map.

public static <
the class of the objects in the set
E
>
NavigableSet<E>

Returns:

a dynamically typesafe view of the specified navigable set
checkedNavigableSet
(NavigableSet<E>
the navigable set for which a dynamically typesafe view is to be returned
s
,
Class<E>
the type of element that s is permitted to hold
type
)

Returns a dynamically typesafe view of the specified navigable set.

public static <
the class of the objects in the queue
E
>
Queue<E>

Returns:

a dynamically typesafe view of the specified queue
checkedQueue
(Queue<E>
the queue for which a dynamically typesafe view is to be returned
queue
,
Class<E>
the type of element that queue is permitted to hold
type
)

Returns a dynamically typesafe view of the specified queue.

public static <
the class of the objects in the set
E
>
Set<E>

Returns:

a dynamically typesafe view of the specified set
checkedSet
(Set<E>
the set for which a dynamically typesafe view is to be returned
s
,
Class<E>
the type of element that s is permitted to hold
type
)

Returns a dynamically typesafe view of the specified set.

public static <
the class of the map keys
K
,
the class of the map values
V
>
SortedMap<K, V>

Returns:

a dynamically typesafe view of the specified map
checkedSortedMap
(SortedMap<K, V>
the map for which a dynamically typesafe view is to be returned
m
,
Class<K>
the type of key that m is permitted to hold
keyType
,
Class<V>
the type of value that m is permitted to hold
valueType
)

Returns a dynamically typesafe view of the specified sorted map.

public static <
the class of the objects in the set
E
>
SortedSet<E>

Returns:

a dynamically typesafe view of the specified sorted set
checkedSortedSet
(SortedSet<E>
the sorted set for which a dynamically typesafe view is to be returned
s
,
Class<E>
the type of element that s is permitted to hold
type
)

Returns a dynamically typesafe view of the specified sorted set.

public static <
the class of the objects in the lists
T
>
void
copy(List<? super T>
The destination list.
dest
,
List<? extends T>
The source list.
src
)

Copies all of the elements from one list into another.

public static boolean

Returns:

true if the two specified collections have no elements in common.
disjoint
(Collection<?>
a collection
c1
,
Collection<?>
a collection
c2
)

Returns true if the two specified collections have no elements in common.

public static <
the class of the objects in the enumeration
T
>
Enumeration<T>

Returns:

an empty enumeration
emptyEnumeration
()

Returns an enumeration that has no elements.

public static <
type of elements, if there were any, in the iterator
T
>
Iterator<T>

Returns:

an empty iterator
emptyIterator
()

Returns an iterator that has no elements.

public static final <
type of elements, if there were any, in the list
T
>
List<T>

Returns:

an empty immutable list
emptyList
()

Returns an empty list (immutable).

public static <
type of elements, if there were any, in the iterator
T
>
ListIterator<T>

Returns:

an empty list iterator
emptyListIterator
()

Returns a list iterator that has no elements.

public static final <
the class of the map keys
K
,
the class of the map values
V
>
Map<K, V>

Returns:

an empty map
emptyMap
()

Returns an empty map (immutable).

public static final <
the class of the map keys
K
,
the class of the map values
V
>
NavigableMap<K, V>

Returns:

an empty navigable map
emptyNavigableMap
()

Returns an empty navigable map (immutable).

public static <
type of elements, if there were any, in the set
E
>
NavigableSet<E>

Returns:

the empty navigable set
emptyNavigableSet
()

Returns an empty navigable set (immutable).

public static final <
the class of the objects in the set
T
>
Set<T>

Returns:

the empty set
emptySet
()

Returns an empty set (immutable).

public static final <
the class of the map keys
K
,
the class of the map values
V
>
SortedMap<K, V>

Returns:

an empty sorted map
emptySortedMap
()

Returns an empty sorted map (immutable).

public static <
type of elements, if there were any, in the set
E
>
SortedSet<E>

Returns:

the empty sorted set
emptySortedSet
()

Returns an empty sorted set (immutable).

public static <
the class of the objects in the collection
T
>
Enumeration<T>

Returns:

an enumeration over the specified collection.
enumeration
(final Collection<T>
the collection for which an enumeration is to be returned.
c
)

Returns an enumeration over the specified collection.

pack-priv static boolean
eq(Object o1, Object o2)

Returns true if the specified arguments are equal, or both null.

public static <
the class of the objects in the list
T
>
void
fill(List<? super T>
the list to be filled with the specified element.
list
,
T
The element with which to fill the specified list.
obj
)

Replaces all of the elements of the specified list with the specified element.

public static int

Returns:

the number of elements in c equal to o
frequency
(Collection<?>
the collection in which to determine the frequency of o
c
,
Object
the object whose frequency is to be determined
o
)

Returns the number of elements in the specified collection equal to the specified object.

private static <T> T
get(ListIterator<? extends T> i, int index)

Gets the ith element from the given list by repositioning the specified list listIterator.

private static <T> int
indexedBinarySearch(List<? extends Comparable<? super T>> list, T key)

private static <T> int
indexedBinarySearch(List<? extends T> l, T key, Comparator<? super T> c)

public static int

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.
indexOfSubList
(List<?>
the list in which to search for the first occurrence of target.
source
,
List<?>
the list to search for as a subList of source.
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.

private static <T> int
iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key)

private static <T> int
iteratorBinarySearch(List<? extends T> l, T key, Comparator<? super T> c)

public static int

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.
lastIndexOfSubList
(List<?>
the list in which to search for the last occurrence of target.
source
,
List<?>
the list to search for as a subList of source.
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.

public static <
the class of the objects returned by the enumeration
T
>
ArrayList<T>

Returns:

an array list containing the elements returned by the specified enumeration.
list
(Enumeration<T>
enumeration providing elements for the returned array list
e
)

Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

public static <
the class of the objects in the collection
T extends Comparable<? super T>
>
T

Returns:

the maximum element of the given collection, according to the natural ordering of its elements.
max
(Collection<? extends T>
the collection whose maximum element is to be determined.
coll
)

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

public static <
the class of the objects in the collection
T
>
T

Returns:

the maximum element of the given collection, according to the specified comparator.
max
(Collection<? extends T>
the collection whose maximum element is to be determined.
coll
,
Comparator<? super T>
the comparator with which to determine the maximum element. A null value indicates that the elements' natural ordering should be used.
comp
)

Returns the maximum element of the given collection, according to the order induced by the specified comparator.

public static <
the class of the objects in the collection
T extends Comparable<? super T>
>
T

Returns:

the minimum element of the given collection, according to the natural ordering of its elements.
min
(Collection<? extends T>
the collection whose minimum element is to be determined.
coll
)

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

public static <
the class of the objects in the collection
T
>
T

Returns:

the minimum element of the given collection, according to the specified comparator.
min
(Collection<? extends T>
the collection whose minimum element is to be determined.
coll
,
Comparator<? super T>
the comparator with which to determine the minimum element. A null value indicates that the elements' natural ordering should be used.
comp
)

Returns the minimum element of the given collection, according to the order induced by the specified comparator.

public static <
the class of the object to copy and of the objects in the returned list.
T
>
List<T>

Returns:

an immutable list consisting of n copies of the specified object.
nCopies
(int
the number of elements in the returned list.
n
,
T
the element to appear repeatedly in the returned list.
o
)

Returns an immutable list consisting of n copies of the specified object.

public static <
the class of the map keys and of the objects in the returned set
E
>
SequencedSet<E>

Returns:

the set backed by the map
newSequencedSetFromMap
(SequencedMap<E, Boolean>
the backing map
map
)

Returns a sequenced set backed by the specified map.

public static <
the class of the map keys and of the objects in the returned set
E
>
Set<E>

Returns:

the set backed by the map
newSetFromMap
(Map<E, Boolean>
the backing map
map
)

Returns a set backed by the specified map.

public static <
the class of the objects in the list
T
>
boolean

Returns:

true if list contained one or more elements e such that (oldVal==null ? e==null : oldVal.equals(e)).
replaceAll
(List<T>
the list in which replacement is to occur.
list
,
T
the old value to be replaced.
oldVal
,
T
the new value with which oldVal is to be replaced.
newVal
)

Replaces all occurrences of one specified value in a list with another.

public static void
reverse(List<?>
the list whose elements are to be reversed.
list
)

Reverses the order of the elements in the specified list.

public static <
the class of the objects compared by the comparator
T
>
Comparator<T>

Returns:

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

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

public static <
the class of the objects compared by the comparator
T
>
Comparator<T>

Returns:

A comparator that imposes the reverse ordering of the specified comparator.
reverseOrder
(Comparator<T>
a comparator who's ordering is to be reversed by the returned comparator or null
cmp
)

Returns a comparator that imposes the reverse ordering of the specified comparator.

public static void
rotate(List<?>
the list to be rotated.
list
,
int
the distance to rotate the list. There are no constraints on this value; it may be zero, negative, or greater than list.size().
distance
)

Rotates the elements in the specified list by the specified distance.

private static <T> void
rotate1(List<T> list, int distance)

private static void
rotate2(List<?> list, int distance)

public static void
shuffle(List<?>
the list to be shuffled.
list
)

Randomly permutes the specified list using a default source of randomness.

public static void
shuffle(List<?>
the list to be shuffled.
list
,
Random
the source of randomness to use to shuffle the list.
rnd
)

Randomly permute the specified list using the specified source of randomness.

public static void
shuffle(List<?>
the list to be shuffled.
list
,
RandomGenerator
the source of randomness to use to shuffle the list.
rnd
)

Randomly permute the specified list using the specified source of randomness.

public static <
the class of the objects in the set
T
>
Set<T>

Returns:

an immutable set containing only the specified object.
singleton
(T
the sole object to be stored in the returned set.
o
)

Returns an immutable set containing only the specified object.

pack-priv static <E> Iterator<E>
singletonIterator(final E e)

public static <
the class of the objects in the list
T
>
List<T>

Returns:

an immutable list containing only the specified object.
singletonList
(T
the sole object to be stored in the returned list.
o
)

Returns an immutable list containing only the specified object.

public static <
the class of the map keys
K
,
the class of the map values
V
>
Map<K, V>

Returns:

an immutable map containing only the specified key-value mapping.
singletonMap
(K
the sole key to be stored in the returned map.
key
,
V
the value to which the returned map maps key.
value
)

Returns an immutable map, mapping only the specified key to the specified value.

pack-priv static <
Type of elements
T
>
Spliterator<T>

Returns:

A singleton Spliterator
singletonSpliterator
(final T element)

Creates a Spliterator with only the specified element

public static <
the class of the objects in the list
T extends Comparable<? super T>
>
void
sort(List<T>
the list to be sorted.
list
)

Sorts the specified list into ascending order, according to the natural ordering of its elements.

public static <
the class of the objects in the list
T
>
void
sort(List<T>
the list to be sorted.
list
,
Comparator<? super T>
the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
c
)

Sorts the specified list according to the order induced by the specified comparator.

public static void
swap(List<?>
The list in which to swap elements.
list
,
int
the index of one element to be swapped.
i
,
int
the index of the other element to be swapped.
j
)

Swaps the elements at the specified positions in the specified list.

private static void
swap(Object[] arr, int i, int j)

Swaps the two specified elements in the specified array.

public static <
the class of the objects in the collection
T
>
Collection<T>

Returns:

a synchronized view of the specified collection.
synchronizedCollection
(Collection<T>
the collection to be "wrapped" in a synchronized collection.
c
)

Returns a synchronized (thread-safe) collection backed by the specified collection.

pack-priv static <T> Collection<T>
public static <
the class of the objects in the list
T
>
List<T>

Returns:

a synchronized view of the specified list.
synchronizedList
(List<T>
the list to be "wrapped" in a synchronized list.
list
)

Returns a synchronized (thread-safe) list backed by the specified list.

pack-priv static <T> List<T>
synchronizedList(List<T> list, Object mutex)

public static <
the class of the map keys
K
,
the class of the map values
V
>
Map<K, V>

Returns:

a synchronized view of the specified map.
synchronizedMap
(Map<K, V>
the map to be "wrapped" in a synchronized map.
m
)

Returns a synchronized (thread-safe) map backed by the specified map.

public static <
the class of the map keys
K
,
the class of the map values
V
>
NavigableMap<K, V>

Returns:

a synchronized view of the specified navigable map.
synchronizedNavigableMap
(NavigableMap<K, V>
the navigable map to be "wrapped" in a synchronized navigable map
m
)

Returns a synchronized (thread-safe) navigable map backed by the specified navigable map.

public static <
the class of the objects in the set
T
>
NavigableSet<T>

Returns:

a synchronized view of the specified navigable set
synchronizedNavigableSet
(NavigableSet<T>
the navigable set to be "wrapped" in a synchronized navigable set
s
)

Returns a synchronized (thread-safe) navigable set backed by the specified navigable set.

public static <
the class of the objects in the set
T
>
Set<T>

Returns:

a synchronized view of the specified set.
synchronizedSet
(Set<T>
the set to be "wrapped" in a synchronized set.
s
)

Returns a synchronized (thread-safe) set backed by the specified set.

pack-priv static <T> Set<T>
synchronizedSet(Set<T> s, Object mutex)

public static <
the class of the map keys
K
,
the class of the map values
V
>
SortedMap<K, V>

Returns:

a synchronized view of the specified sorted map.
synchronizedSortedMap
(SortedMap<K, V>
the sorted map to be "wrapped" in a synchronized sorted map.
m
)

Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

public static <
the class of the objects in the set
T
>
SortedSet<T>

Returns:

a synchronized view of the specified sorted set.
synchronizedSortedSet
(SortedSet<T>
the sorted set to be "wrapped" in a synchronized sorted set.
s
)

Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

public static <
the class of the objects in the collection
T
>
Collection<T>

Returns:

an unmodifiable view of the specified collection.
unmodifiableCollection
(Collection<? extends T>
the collection for which an unmodifiable view is to be returned.
c
)

Returns an unmodifiable view of the specified collection.

public static <
the class of the objects in the list
T
>
List<T>

Returns:

an unmodifiable view of the specified list.
unmodifiableList
(List<? extends T>
the list for which an unmodifiable view is to be returned.
list
)

Returns an unmodifiable view of the specified list.

public static <
the class of the map keys
K
,
the class of the map values
V
>
Map<K, V>

Returns:

an unmodifiable view of the specified map.
unmodifiableMap
(Map<? extends K, ? extends V>
the map for which an unmodifiable view is to be returned.
m
)

Returns an unmodifiable view of the specified map.

public static <
the class of the map keys
K
,
the class of the map values
V
>
NavigableMap<K, V>

Returns:

an unmodifiable view of the specified navigable map
unmodifiableNavigableMap
(NavigableMap<K, ? extends V>
the navigable map for which an unmodifiable view is to be returned
m
)

Returns an unmodifiable view of the specified navigable map.

public static <
the class of the objects in the set
T
>
NavigableSet<T>

Returns:

an unmodifiable view of the specified navigable set
unmodifiableNavigableSet
(NavigableSet<T>
the navigable set for which an unmodifiable view is to be returned
s
)

Returns an unmodifiable view of the specified navigable set.

public static <
the class of the objects in the collection
T
>
SequencedCollection<T>

Returns:

an unmodifiable view of the specified collection.
unmodifiableSequencedCollection
(SequencedCollection<? extends T>
the collection for which an unmodifiable view is to be returned.
c
)

Returns an unmodifiable view of the specified SequencedCollection.

public static <
the class of the map keys
K
,
the class of the map values
V
>
SequencedMap<K, V>

Returns:

an unmodifiable view of the specified map.
unmodifiableSequencedMap
(SequencedMap<? extends K, ? extends V>
the map for which an unmodifiable view is to be returned.
m
)

Returns an unmodifiable view of the specified SequencedMap.

public static <
the class of the objects in the set
T
>
SequencedSet<T>

Returns:

an unmodifiable view of the specified sequenced set.
unmodifiableSequencedSet
(SequencedSet<? extends T>
the set for which an unmodifiable view is to be returned.
s
)

Returns an unmodifiable view of the specified SequencedSet.

public static <
the class of the objects in the set
T
>
Set<T>

Returns:

an unmodifiable view of the specified set.
unmodifiableSet
(Set<? extends T>
the set for which an unmodifiable view is to be returned.
s
)

Returns an unmodifiable view of the specified set.

public static <
the class of the map keys
K
,
the class of the map values
V
>
SortedMap<K, V>

Returns:

an unmodifiable view of the specified sorted map.
unmodifiableSortedMap
(SortedMap<K, ? extends V>
the sorted map for which an unmodifiable view is to be returned.
m
)

Returns an unmodifiable view of the specified sorted map.

public static <
the class of the objects in the set
T
>
SortedSet<T>

Returns:

an unmodifiable view of the specified sorted set.
unmodifiableSortedSet
(SortedSet<T>
the sorted set for which an unmodifiable view is to be returned.
s
)

Returns an unmodifiable view of the specified sorted set.

pack-priv static <T> T[]
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait