GeezerVison is showing docs.oracle.com — Collections (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util
Class Collections
-
-
Method Summary
All Methods Static Methods Concrete Methods
| Modifier and Type |
Method |
Description |
static <T> boolean |
addAll(Collection<? super T> c,
T... elements) |
Adds all of the specified elements to the specified collection.
|
static <T> Queue<T> |
asLifoQueue(Deque<T> deque) |
Returns a view of a Deque as a Last-in-first-out (Lifo)
Queue.
|
static <T> int |
binarySearch(List<? extends Comparable<? super T>> list,
T key) |
Searches the specified list for the specified object using the binary
search algorithm.
|
static <T> int |
binarySearch(List<? extends T> list,
T key,
Comparator<? super T> c) |
Searches the specified list for the specified object using the binary
search algorithm.
|
static <E> Collection<E> |
checkedCollection(Collection<E> c,
Class<E> type) |
Returns a dynamically typesafe view of the specified collection.
|
static <E> List<E> |
checkedList(List<E> list,
Class<E> type) |
Returns a dynamically typesafe view of the specified list.
|
static <K,V> Map<K,V> |
checkedMap(Map<K,V> m,
Class<K> keyType,
Class<V> valueType) |
Returns a dynamically typesafe view of the specified map.
|
static <K,V> NavigableMap<K,V> |
checkedNavigableMap(NavigableMap<K,V> m,
Class<K> keyType,
Class<V> valueType) |
Returns a dynamically typesafe view of the specified navigable map.
|
static <E> NavigableSet<E> |
checkedNavigableSet(NavigableSet<E> s,
Class<E> type) |
Returns a dynamically typesafe view of the specified navigable set.
|
static <E> Queue<E> |
checkedQueue(Queue<E> queue,
Class<E> type) |
Returns a dynamically typesafe view of the specified queue.
|
static <E> Set<E> |
checkedSet(Set<E> s,
Class<E> type) |
Returns a dynamically typesafe view of the specified set.
|
static <K,V> SortedMap<K,V> |
checkedSortedMap(SortedMap<K,V> m,
Class<K> keyType,
Class<V> valueType) |
Returns a dynamically typesafe view of the specified sorted map.
|
static <E> SortedSet<E> |
checkedSortedSet(SortedSet<E> s,
Class<E> type) |
Returns a dynamically typesafe view of the specified sorted set.
|
static <T> void |
copy(List<? super T> dest,
List<? extends T> src) |
Copies all of the elements from one list into another.
|
static boolean |
disjoint(Collection<?> c1,
Collection<?> c2) |
Returns true if the two specified collections have no
elements in common.
|
static <T> Enumeration<T> |
emptyEnumeration() |
Returns an enumeration that has no elements.
|
static <T> Iterator<T> |
emptyIterator() |
Returns an iterator that has no elements.
|
static <T> List<T> |
emptyList() |
Returns an empty list (immutable).
|
static <T> ListIterator<T> |
emptyListIterator() |
Returns a list iterator that has no elements.
|
static <K,V> Map<K,V> |
emptyMap() |
Returns an empty map (immutable).
|
static <K,V> NavigableMap<K,V> |
emptyNavigableMap() |
Returns an empty navigable map (immutable).
|
static <E> NavigableSet<E> |
emptyNavigableSet() |
Returns an empty navigable set (immutable).
|
static <T> Set<T> |
emptySet() |
Returns an empty set (immutable).
|
static <K,V> SortedMap<K,V> |
emptySortedMap() |
Returns an empty sorted map (immutable).
|
static <E> SortedSet<E> |
emptySortedSet() |
Returns an empty sorted set (immutable).
|
static <T> Enumeration<T> |
enumeration(Collection<T> c) |
Returns an enumeration over the specified collection.
|
static <T> void |
fill(List<? super T> list,
T obj) |
Replaces all of the elements of the specified list with the specified
element.
|
static int |
frequency(Collection<?> c,
Object o) |
Returns the number of elements in the specified collection equal to the
specified object.
|
static int |
indexOfSubList(List<?> source,
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(List<?> source,
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<T> |
list(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 & Comparable<? super T>> T |
max(Collection<? extends T> coll) |
Returns the maximum element of the given collection, according to the
natural ordering of its elements.
|
static <T> T |
max(Collection<? extends T> coll,
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 & Comparable<? super T>> T |
min(Collection<? extends T> coll) |
Returns the minimum element of the given collection, according to the
natural ordering of its elements.
|
static <T> T |
min(Collection<? extends T> coll,
Comparator<? super T> comp) |
Returns the minimum element of the given collection, according to the
order induced by the specified comparator.
|
static <T> List<T> |
nCopies(int n,
T o) |
Returns an immutable list consisting of n copies of the
specified object.
|
static <E> Set<E> |
newSetFromMap(Map<E,Boolean> map) |
Returns a set backed by the specified map.
|
static <T> boolean |
replaceAll(List<T> list,
T oldVal,
T newVal) |
Replaces all occurrences of one specified value in a list with another.
|
static void |
reverse(List<?> list) |
Reverses the order of the elements in the specified list.
|
static <T> Comparator<T> |
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<T> |
reverseOrder(Comparator<T> cmp) |
Returns a comparator that imposes the reverse ordering of the specified
comparator.
|
static void |
rotate(List<?> list,
int distance) |
Rotates the elements in the specified list by the specified distance.
|
static void |
shuffle(List<?> list) |
Randomly permutes the specified list using a default source of
randomness.
|
static void |
shuffle(List<?> list,
Random rnd) |
Randomly permute the specified list using the specified source of
randomness.
|
static <T> Set<T> |
singleton(T o) |
Returns an immutable set containing only the specified object.
|
static <T> List<T> |
singletonList(T o) |
Returns an immutable list containing only the specified object.
|
static <K,V> Map<K,V> |
singletonMap(K key,
V value) |
Returns an immutable map, mapping only the specified key to the
specified value.
|
static <T extends Comparable<? super T>> void |
sort(List<T> list) |
Sorts the specified list into ascending order, according to the
natural ordering of its elements.
|
static <T> void |
sort(List<T> list,
Comparator<? super T> c) |
Sorts the specified list according to the order induced by the
specified comparator.
|
static void |
swap(List<?> list,
int i,
int j) |
Swaps the elements at the specified positions in the specified list.
|
static <T> Collection<T> |
synchronizedCollection(Collection<T> c) |
Returns a synchronized (thread-safe) collection backed by the specified
collection.
|
static <T> List<T> |
synchronizedList(List<T> list) |
Returns a synchronized (thread-safe) list backed by the specified
list.
|
static <K,V> Map<K,V> |
synchronizedMap(Map<K,V> m) |
Returns a synchronized (thread-safe) map backed by the specified
map.
|
static <K,V> NavigableMap<K,V> |
synchronizedNavigableMap(NavigableMap<K,V> m) |
Returns a synchronized (thread-safe) navigable map backed by the
specified navigable map.
|
static <T> NavigableSet<T> |
synchronizedNavigableSet(NavigableSet<T> s) |
Returns a synchronized (thread-safe) navigable set backed by the
specified navigable set.
|
static <T> Set<T> |
synchronizedSet(Set<T> s) |
Returns a synchronized (thread-safe) set backed by the specified
set.
|
static <K,V> SortedMap<K,V> |
synchronizedSortedMap(SortedMap<K,V> m) |
Returns a synchronized (thread-safe) sorted map backed by the specified
sorted map.
|
static <T> SortedSet<T> |
synchronizedSortedSet(SortedSet<T> s) |
Returns a synchronized (thread-safe) sorted set backed by the specified
sorted set.
|
static <T> Collection<T> |
unmodifiableCollection(Collection<? extends T> c) |
Returns an unmodifiable view of the specified collection.
|
static <T> List<T> |
unmodifiableList(List<? extends T> list) |
Returns an unmodifiable view of the specified list.
|
static <K,V> Map<K,V> |
unmodifiableMap(Map<? extends K,? extends V> m) |
Returns an unmodifiable view of the specified map.
|
static <K,V> NavigableMap<K,V> |
unmodifiableNavigableMap(NavigableMap<K,? extends V> m) |
Returns an unmodifiable view of the specified navigable map.
|
static <T> NavigableSet<T> |
unmodifiableNavigableSet(NavigableSet<T> s) |
Returns an unmodifiable view of the specified navigable set.
|
static <T> Set<T> |
unmodifiableSet(Set<? extends T> s) |
Returns an unmodifiable view of the specified set.
|
static <K,V> SortedMap<K,V> |
unmodifiableSortedMap(SortedMap<K,? extends V> m) |
Returns an unmodifiable view of the specified sorted map.
|
static <T> SortedSet<T> |
unmodifiableSortedSet(SortedSet<T> s) |
Returns an unmodifiable view of the specified sorted set.
|
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
swap
public static void swap(List<?> list,
int i,
int j)
Swaps the elements at the specified positions in the specified list.
(If the specified positions are equal, invoking this method leaves
the list unchanged.)
- Parameters:
list - The list in which to swap elements.
i - the index of one element to be swapped.
j - the index of the other element to be swapped.
- Throws:
IndexOutOfBoundsException - if either i or j
is out of range (i < 0 || i >= list.size()
|| j < 0 || j >= list.size()).
- Since:
- 1.4
-
replaceAll
public static <T> boolean replaceAll(List<T> list,
T oldVal,
T newVal)
Replaces all occurrences of one specified value in a list with another.
More formally, replaces with newVal each element e
in list such that
(oldVal==null ? e==null : oldVal.equals(e)).
(This method has no effect on the size of the list.)
- Type Parameters:
T - the class of the objects in the list
- Parameters:
list - the list in which replacement is to occur.
oldVal - the old value to be replaced.
newVal - the new value with which oldVal is to be
replaced.
- Returns:
- true if list contained one or more elements
e such that
(oldVal==null ? e==null : oldVal.equals(e)).
- Throws:
UnsupportedOperationException - if the specified list or
its list-iterator does not support the set operation.
- Since:
- 1.4
-
checkedMap
public static <K,V> Map<K,V> checkedMap(Map<K,V> m,
Class<K> keyType,
Class<V> valueType)
Returns a dynamically typesafe view of the specified map.
Any attempt to insert a mapping whose key or value have the wrong
type will result in an immediate ClassCastException.
Similarly, any attempt to modify the value currently associated with
a key will result in an immediate ClassCastException,
whether the modification is attempted directly through the map
itself, or through a Map.Entry instance obtained from the
map's entry set view.
Assuming a map contains no incorrectly typed keys or values
prior to the time a dynamically typesafe view is generated, and
that all subsequent access to the map takes place through the view
(or one of its collection views), it is guaranteed that the
map cannot contain an incorrectly typed key or value.
A discussion of the use of dynamically typesafe views may be
found in the documentation for the checkedCollection method.
The returned map will be serializable if the specified map is
serializable.
Since null is considered to be a value of any reference
type, the returned map permits insertion of null keys or values
whenever the backing map does.
- Type Parameters:
K - the class of the map keys
V - the class of the map values
- Parameters:
m - the map for which a dynamically typesafe view is to be
returned
keyType - the type of key that m is permitted to hold
valueType - the type of value that m is permitted to hold
- Returns:
- a dynamically typesafe view of the specified map
- Since:
- 1.5
-
checkedSortedMap
public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m,
Class<K> keyType,
Class<V> valueType)
Returns a dynamically typesafe view of the specified sorted map.
Any attempt to insert a mapping whose key or value have the wrong
type will result in an immediate ClassCastException.
Similarly, any attempt to modify the value currently associated with
a key will result in an immediate ClassCastException,
whether the modification is attempted directly through the map
itself, or through a Map.Entry instance obtained from the
map's entry set view.
Assuming a map contains no incorrectly typed keys or values
prior to the time a dynamically typesafe view is generated, and
that all subsequent access to the map takes place through the view
(or one of its collection views), it is guaranteed that the
map cannot contain an incorrectly typed key or value.
A discussion of the use of dynamically typesafe views may be
found in the documentation for the checkedCollection method.
The returned map will be serializable if the specified map is
serializable.
Since null is considered to be a value of any reference
type, the returned map permits insertion of null keys or values
whenever the backing map does.
- Type Parameters:
K - the class of the map keys
V - the class of the map values
- Parameters:
m - the map for which a dynamically typesafe view is to be
returned
keyType - the type of key that m is permitted to hold
valueType - the type of value that m is permitted to hold
- Returns:
- a dynamically typesafe view of the specified map
- Since:
- 1.5
-
checkedNavigableMap
public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K,V> m,
Class<K> keyType,
Class<V> valueType)
Returns a dynamically typesafe view of the specified navigable map.
Any attempt to insert a mapping whose key or value have the wrong
type will result in an immediate ClassCastException.
Similarly, any attempt to modify the value currently associated with
a key will result in an immediate ClassCastException,
whether the modification is attempted directly through the map
itself, or through a Map.Entry instance obtained from the
map's entry set view.
Assuming a map contains no incorrectly typed keys or values
prior to the time a dynamically typesafe view is generated, and
that all subsequent access to the map takes place through the view
(or one of its collection views), it is guaranteed that the
map cannot contain an incorrectly typed key or value.
A discussion of the use of dynamically typesafe views may be
found in the documentation for the checkedCollection method.
The returned map will be serializable if the specified map is
serializable.
Since null is considered to be a value of any reference
type, the returned map permits insertion of null keys or values
whenever the backing map does.
- Type Parameters:
K - type of map keys
V - type of map values
- Parameters:
m - the map for which a dynamically typesafe view is to be
returned
keyType - the type of key that m is permitted to hold
valueType - the type of value that m is permitted to hold
- Returns:
- a dynamically typesafe view of the specified map
- Since:
- 1.8
-
singletonMap
public static <K,V> Map<K,V> singletonMap(K key,
V value)
Returns an immutable map, mapping only the specified key to the
specified value. The returned map is serializable.
- Type Parameters:
K - the class of the map keys
V - the class of the map values
- Parameters:
key - the sole key to be stored in the returned map.
value - the value to which the returned map maps key.
- Returns:
- an immutable map containing only the specified key-value
mapping.
- Since:
- 1.3
-
nCopies
public static <T> List<T> nCopies(int n,
T o)
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.
- Type Parameters:
T - the class of the object to copy and of the objects
in the returned list.
- Parameters:
n - the number of elements in the returned list.
o - the element to appear repeatedly in the returned list.
- Returns:
- an immutable list consisting of n copies of the
specified object.
- Throws:
IllegalArgumentException - if n < 0
- See Also:
List.addAll(Collection),
List.addAll(int, Collection)
-
list
public static <T> ArrayList<T> list(Enumeration<T> e)
Returns an array list containing the elements returned by the
specified enumeration in the order they are returned by the
enumeration. This method provides interoperability between
legacy APIs that return enumerations and new APIs that require
collections.
- Type Parameters:
T - the class of the objects returned by the enumeration
- Parameters:
e - enumeration providing elements for the returned
array list
- Returns:
- an array list containing the elements returned
by the specified enumeration.
- Since:
- 1.4
- See Also:
Enumeration,
ArrayList
-
frequency
public static int frequency(Collection<?> c,
Object o)
Returns the number of elements in the specified collection equal to the
specified object. More formally, returns the number of elements
e in the collection such that
(o == null ? e == null : o.equals(e)).
- Parameters:
c - the collection in which to determine the frequency
of o
o - the object whose frequency is to be determined
- Returns:
- the number of elements in
c equal to o
- Throws:
NullPointerException - if c is null
- Since:
- 1.5
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|