Package java.util
Class Collections
java.lang.Object
java.util.Collections
public class Collections extends Object
Collections contains static methods which operate on
Collection classes.- Since:
- 1.2
-
Field Summary
-
Method Summary
Modifier and Type Method Description static <T> booleanaddAll(Collection<? super T> c, T... a)Adds all the specified elements to the specified collection.static <T> Queue<T>asLifoQueue(Deque<T> deque)Returns a last-in, first-out queue as a view ofdeque.static <T> intbinarySearch(List<? extends Comparable<? super T>> list, T object)Performs a binary search for the specified element in the specified sorted list.static <T> intbinarySearch(List<? extends T> list, T object, Comparator<? super T> comparator)Performs a binary search for the specified element in the specified sorted list using the specified comparator.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 <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> voidcopy(List<? super T> destination, List<? extends T> source)Copies the elements from the source list to the destination list.static booleandisjoint(Collection<?> c1, Collection<?> c2)Returns whether the specified collections have no elements in common.static <T> Enumeration<T>emptyEnumeration()Returns an enumeration containing no elements.static <T> Iterator<T>emptyIterator()Returns an iterator containing no elements.static <T> List<T>emptyList()Returns a type-safe empty, immutableList.static <T> ListIterator<T>emptyListIterator()Returns a list iterator containing no elements.static <K, V> Map<K,V>emptyMap()Returns a type-safe empty, immutableMap.static <T> Set<T>emptySet()Returns a type-safe empty, immutableSet.static <T> Enumeration<T>enumeration(Collection<T> collection)Returns anEnumerationon the specified collection.static <T> voidfill(List<? super T> list, T object)Fills the specified list with the specified element.static intfrequency(Collection<?> c, Object o)Returns the number of elements in theCollectionthat match theObjectpassed.static intindexOfSubList(List<?> list, List<?> sublist)Searches thelistforsublistand returns the beginning index of the first occurrence.static intlastIndexOfSubList(List<?> list, List<?> sublist)Searches thelistforsublistand returns the beginning index of the last occurrence.static <T> ArrayList<T>list(Enumeration<T> enumeration)Returns anArrayListwith all the elements in theenumeration.static <T extends Object & Comparable<? super T>>
Tmax(Collection<? extends T> collection)Searches the specified collection for the maximum element.static <T> Tmax(Collection<? extends T> collection, Comparator<? super T> comparator)Searches the specified collection for the maximum element using the specified comparator.static <T extends Object & Comparable<? super T>>
Tmin(Collection<? extends T> collection)Searches the specified collection for the minimum element.static <T> Tmin(Collection<? extends T> collection, Comparator<? super T> comparator)Searches the specified collection for the minimum element using the specified comparator.static <T> List<T>nCopies(int length, T object)Returns a list containing the specified number of the specified element.static <E> Set<E>newSetFromMap(Map<E,Boolean> map)Returns a set backed bymap.static <T> booleanreplaceAll(List<T> list, T obj, T obj2)Replaces all occurrences of ObjectobjinlistwithnewObj.static voidreverse(List<?> list)Modifies the specifiedListby reversing the order of the elements.static <T> Comparator<T>reverseOrder()A comparator which reverses the natural order of the elements.static <T> Comparator<T>reverseOrder(Comparator<T> c)Returns aComparatorthat reverses the order of theComparatorpassed.static voidrotate(List<?> lst, int dist)Rotates the elements inlistby the distancediststatic introundUpToPowerOfTwo(int i)Returns the smallest power of two >= its argument, with several caveats: If the argument is negative but not Integer.MIN_VALUE, the method returns zero.static intsecondaryHash(Object key)Computes a hash code and applies a supplemental hash function to defend against poor quality hash functions.static intsecondaryIdentityHash(Object key)Computes an identity hash code and applies a supplemental hash function to defend against poor quality hash functions.static voidshuffle(List<?> list)Moves every element of the list to a random new position in the list.static voidshuffle(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 <E> Set<E>singleton(E object)Returns a set containing the specified element.static <E> List<E>singletonList(E object)Returns a list containing the specified element.static <K, V> Map<K,V>singletonMap(K key, V value)Returns a Map containing the specified key and value.static <T extends Comparable<? super T>>
voidsort(List<T> list)Sorts the given list in ascending natural order.static <T> voidsort(List<T> list, Comparator<? super T> comparator)Sorts the given list using the given comparator.static voidswap(List<?> list, int index1, int index2)Swaps the elements of listlistat indicesindex1andindex2.static <T> Collection<T>synchronizedCollection(Collection<T> collection)Returns a wrapper on the specified collection which synchronizes all access to the collection.static <T> List<T>synchronizedList(List<T> list)Returns a wrapper on the specified List which synchronizes all access to the List.static <K, V> Map<K,V>synchronizedMap(Map<K,V> map)Returns a wrapper on the specified map which synchronizes all access to the map.static <E> Set<E>synchronizedSet(Set<E> set)Returns a wrapper on the specified set which synchronizes all access to the set.static <K, V> SortedMap<K,V>synchronizedSortedMap(SortedMap<K,V> map)Returns a wrapper on the specified sorted map which synchronizes all access to the sorted map.static <E> SortedSet<E>synchronizedSortedSet(SortedSet<E> set)Returns a wrapper on the specified sorted set which synchronizes all access to the sorted set.static <E> Collection<E>unmodifiableCollection(Collection<? extends E> collection)Returns a wrapper on the specified collection which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the collection.static <E> List<E>unmodifiableList(List<? extends E> list)Returns a wrapper on the specified list which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the list.static <K, V> Map<K,V>unmodifiableMap(Map<? extends K,? extends V> map)Returns a wrapper on the specified map which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the map.static <E> Set<E>unmodifiableSet(Set<? extends E> set)Returns a wrapper on the specified set which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the set.static <K, V> SortedMap<K,V>unmodifiableSortedMap(SortedMap<K,? extends V> map)Returns a wrapper on the specified sorted map which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the sorted map.static <E> SortedSet<E>unmodifiableSortedSet(SortedSet<E> set)Returns a wrapper on the specified sorted set which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the sorted set.
-
Field Details
-
Method Details
-
binarySearch
Performs a binary search for the specified element in the specified sorted list. The list needs to be already sorted in natural sorting order. Searching in an unsorted array has an undefined result. It's also undefined which element is found if there are multiple occurrences of the same element.- 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 - 1where the element would be inserted - Throws:
ClassCastException- if an element in the List or the search element does not implement Comparable, or cannot be compared to each other.
-
binarySearch
public static <T> int binarySearch(List<? extends T> list, T object, Comparator<? super T> comparator)Performs a binary search for the specified element in the specified sorted list using the specified comparator. The list needs to be already sorted according to the comparator passed. Searching in an unsorted array has an undefined result. It's also undefined which element is found if there are multiple occurrences of the same element.- Parameters:
list- the sorted List to search.object- the element to find.comparator- the comparator. If the comparator isnullthen the search uses the objects' natural ordering.- Returns:
- the non-negative index of the element, or a negative index which
is the
-index - 1where 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
Copies the elements from the source list to the destination list. At the end both lists will have the same objects at the same index. If the destination array is larger than the source list, the elements in the destination list withindex >= source.size()will be unchanged.- Parameters:
destination- the list whose elements are set from the source list.source- the list with the elements to be copied into the destination.- 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
Returns anEnumerationon the specified collection.- Parameters:
collection- the collection to enumerate.- Returns:
- an Enumeration.
-
fill
Fills the specified list with the specified element.- Parameters:
list- the list to fill.object- the element to fill the list with.- Throws:
UnsupportedOperationException- when replacing an element in the List is not supported.
-
max
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 implementComparableor elements cannot be compared to each other.
-
max
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 theComparator.
-
min
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 implementComparableor elements cannot be compared to each other.
-
min
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 theComparator.
-
nCopies
Returns a list containing the specified number of the specified element. The list cannot be modified. The list is serializable.- Parameters:
length- the size of the returned list.object- the element to be addedlengthtimes to a list.- Returns:
- a list containing
lengthcopies of the element. - Throws:
IllegalArgumentException- whenlength < 0.
-
reverse
Modifies the specifiedListby reversing the order of the elements.- Parameters:
list- the list to reverse.- Throws:
UnsupportedOperationException- when replacing an element in the List is not supported.
-
reverseOrder
A comparator which reverses the natural order of the elements. TheComparatorthat's returned isSerializable.- Returns:
- a
Comparatorinstance.
-
reverseOrder
Returns aComparatorthat reverses the order of theComparatorpassed. If theComparatorpassed isnull, then this method is equivalent toreverseOrder().The
Comparatorthat's returned isSerializableif theComparatorpassed is serializable ornull.- Parameters:
c- theComparatorto reverse ornull.- Returns:
- a
Comparatorinstance. - Since:
- 1.5
-
shuffle
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
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
Returns a set containing the specified element. The set cannot be modified. The set is serializable.- Parameters:
object- the element.- Returns:
- a set containing the element.
-
singletonList
Returns a list containing the specified element. The list cannot be modified. The list is serializable.- Parameters:
object- the element.- Returns:
- a list containing the element.
-
singletonMap
Returns a Map containing the specified key and value. The map cannot be modified. The map is serializable.- Parameters:
key- the key.value- the value.- Returns:
- a Map containing the key and value.
-
sort
Sorts the given list in ascending natural order. The algorithm is stable which means equal elements don't get reordered.- Throws:
ClassCastException- if any element does not implementComparable, or ifcompareTothrows for any pair of elements.
-
sort
Sorts the given list using the given comparator. The algorithm is stable which means equal elements don't get reordered.- Throws:
ClassCastException- if any element does not implementComparable, or ifcompareTothrows for any pair of elements.
-
swap
Swaps the elements of listlistat indicesindex1andindex2.- Parameters:
list- the list to manipulate.index1- position of the first element to swap with the element in index2.index2- position of the other element.- Throws:
IndexOutOfBoundsException- if index1 or index2 is out of range of this list.- Since:
- 1.4
-
replaceAll
Replaces all occurrences of ObjectobjinlistwithnewObj. If theobjisnull, then all occurrences ofnullare replaced withnewObj.- Parameters:
list- the list to modify.obj- the object to find and replace occurrences of.obj2- the object to replace all occurrences ofobjinlist.- Returns:
- true, if at least one occurrence of
objhas been found inlist. - Throws:
UnsupportedOperationException- if the list does not support setting elements.
-
rotate
Rotates the elements inlistby the distancediste.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:
lst- the list whose elements are to be rotated.dist- is the distance the list is rotated. This can be any valid integer. Negative values rotate the list backwards.
-
indexOfSubList
Searches thelistforsublistand returns the beginning index of the first occurrence.-1 is returned if the
sublistdoes not exist inlist.- Parameters:
list- the List to searchsublistin.sublist- the List to search inlist.- Returns:
- the beginning index of the first occurrence of
sublistinlist, or -1.
-
lastIndexOfSubList
Searches thelistforsublistand returns the beginning index of the last occurrence.-1 is returned if the
sublistdoes not exist inlist.- Parameters:
list- the list to searchsublistin.sublist- the list to search inlist.- Returns:
- the beginning index of the last occurrence of
sublistinlist, or -1.
-
list
Returns anArrayListwith all the elements in theenumeration. The elements in the returnedArrayListare in the same order as in theenumeration.- Parameters:
enumeration- the sourceEnumeration.- Returns:
- an
ArrayListfromenumeration.
-
synchronizedCollection
Returns a wrapper on the specified collection which synchronizes all access to the collection.- Parameters:
collection- the Collection to wrap in a synchronized collection.- Returns:
- a synchronized Collection.
-
synchronizedList
Returns a wrapper on the specified List which synchronizes all access to the List.- Parameters:
list- the List to wrap in a synchronized list.- Returns:
- a synchronized List.
-
synchronizedMap
Returns a wrapper on the specified map which synchronizes all access to the map.- Parameters:
map- the map to wrap in a synchronized map.- Returns:
- a synchronized Map.
-
synchronizedSet
Returns a wrapper on the specified set which synchronizes all access to the set.- Parameters:
set- the set to wrap in a synchronized set.- Returns:
- a synchronized set.
-
synchronizedSortedMap
Returns a wrapper on the specified sorted map which synchronizes all access to the sorted map.- Parameters:
map- the sorted map to wrap in a synchronized sorted map.- Returns:
- a synchronized sorted map.
-
synchronizedSortedSet
Returns a wrapper on the specified sorted set which synchronizes all access to the sorted set.- Parameters:
set- the sorted set to wrap in a synchronized sorted set.- Returns:
- a synchronized sorted set.
-
unmodifiableCollection
Returns a wrapper on the specified collection which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the collection.- Parameters:
collection- the collection to wrap in an unmodifiable collection.- Returns:
- an unmodifiable collection.
-
unmodifiableList
Returns a wrapper on the specified list which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the list.- Parameters:
list- the list to wrap in an unmodifiable list.- Returns:
- an unmodifiable List.
-
unmodifiableMap
Returns a wrapper on the specified map which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the map.- Parameters:
map- the map to wrap in an unmodifiable map.- Returns:
- a unmodifiable map.
-
unmodifiableSet
Returns a wrapper on the specified set which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the set.- Parameters:
set- the set to wrap in an unmodifiable set.- Returns:
- a unmodifiable set
-
unmodifiableSortedMap
Returns a wrapper on the specified sorted map which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the sorted map.- Parameters:
map- the sorted map to wrap in an unmodifiable sorted map.- Returns:
- a unmodifiable sorted map
-
unmodifiableSortedSet
Returns a wrapper on the specified sorted set which throws anUnsupportedOperationExceptionwhenever an attempt is made to modify the sorted set.- Parameters:
set- the sorted set to wrap in an unmodifiable sorted set.- Returns:
- a unmodifiable sorted set.
-
frequency
Returns the number of elements in theCollectionthat match theObjectpassed. If theObjectisnull, then the number ofnullelements is returned.- Parameters:
c- theCollectionto search.o- theObjectto search for.- Returns:
- the number of matching elements.
- Throws:
NullPointerException- if theCollectionparameter isnull.- Since:
- 1.5
-
emptyList
Returns a type-safe empty, immutableList.- Returns:
- an empty
List. - Since:
- 1.5
- See Also:
EMPTY_LIST
-
emptySet
Returns a type-safe empty, immutableSet. -
emptyMap
Returns a type-safe empty, immutableMap. -
emptyEnumeration
Returns an enumeration containing no elements.- Since:
- 1.7
-
emptyIterator
Returns an iterator containing no elements.- Since:
- 1.7
-
emptyListIterator
Returns a list iterator containing no elements.- Since:
- 1.7
-
checkedCollection
Returns a dynamically typesafe view of the specified collection. Trying to insert an element of the wrong type into this collection throws aClassCastException. At creation time the types incare not checked for correct type.- Parameters:
c- the collection to be wrapped in a typesafe collection.type- the type of the elements permitted to insert.- Returns:
- a typesafe collection.
-
checkedMap
Returns a dynamically typesafe view of the specified map. Trying to insert an element of the wrong type into this map throws aClassCastException. At creation time the types inmare not checked for correct type.- Parameters:
m- the map to be wrapped in a typesafe map.keyType- the type of the keys permitted to insert.valueType- the type of the values permitted to insert.- Returns:
- a typesafe map.
-
checkedList
Returns a dynamically typesafe view of the specified list. Trying to insert an element of the wrong type into this list throws aClassCastException. At creation time the types inlistare not checked for correct type.- Parameters:
list- the list to be wrapped in a typesafe list.type- the type of the elements permitted to insert.- Returns:
- a typesafe list.
-
checkedSet
Returns a dynamically typesafe view of the specified set. Trying to insert an element of the wrong type into this set throws aClassCastException. At creation time the types insare not checked for correct type.- Parameters:
s- the set to be wrapped in a typesafe set.type- the type of the elements permitted to insert.- Returns:
- a typesafe set.
-
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. Trying to insert an element of the wrong type into this sorted map throws aClassCastException. At creation time the types inmare not checked for correct type.- Parameters:
m- the sorted map to be wrapped in a typesafe sorted map.keyType- the type of the keys permitted to insert.valueType- the type of the values permitted to insert.- Returns:
- a typesafe sorted map.
-
checkedSortedSet
Returns a dynamically typesafe view of the specified sorted set. Trying to insert an element of the wrong type into this sorted set throws aClassCastException. At creation time the types insare not checked for correct type.- Parameters:
s- the sorted set to be wrapped in a typesafe sorted set.type- the type of the elements permitted to insert.- Returns:
- a typesafe sorted set.
-
addAll
Adds all the specified elements to the specified collection.- Parameters:
c- the collection the elements are to be inserted into.a- the elements to insert.- Returns:
- true if the collection changed during insertion.
- Throws:
UnsupportedOperationException- when the method is not supported.NullPointerException- whencoraisnull, oracontains one or morenullelements andcdoesn't supportnullelements.IllegalArgumentException- if at least one of the elements can't be inserted into the collection.
-
disjoint
Returns whether the specified collections have no elements in common.- Parameters:
c1- the first collection.c2- the second collection.- Returns:
trueif the collections have no elements in common,falseotherwise.- Throws:
NullPointerException- if one of the collections isnull.
-
newSetFromMap
Returns a set backed bymap.- Throws:
IllegalArgumentException- if the map is not empty- Since:
- 1.6
-
asLifoQueue
Returns a last-in, first-out queue as a view ofdeque.- Since:
- 1.6
-
secondaryHash
Computes a hash code and applies a supplemental hash function to defend against poor quality hash functions. This is critical because HashMap uses power-of-two length hash tables, that otherwise encounter collisions for hash codes that do not differ in lower or upper bits. Routine taken from java.util.concurrent.ConcurrentHashMap.hash(int). -
secondaryIdentityHash
Computes an identity hash code and applies a supplemental hash function to defend against poor quality hash functions. This is critical because identity hash codes are currently implemented as object addresses, which will have been aligned by the underlying memory allocator causing all hash codes to have the same bottom bits. -
roundUpToPowerOfTwo
public static int roundUpToPowerOfTwo(int i)Returns the smallest power of two >= its argument, with several caveats: If the argument is negative but not Integer.MIN_VALUE, the method returns zero. If the argument is > 2^30 or equal to Integer.MIN_VALUE, the method returns Integer.MIN_VALUE. If the argument is zero, the method returns zero.
-