public class Collections
- Object
- Collections
Collections contains static methods which operate on
Collection classes.Fields
public static final List EMPTY_LIST | An empty immutable instance of List. |
public static final Set EMPTY_SET | An empty immutable instance of Set. |
public static final Map EMPTY_MAP | An empty immutable instance of Map. |
Methods
Inherited methods
Field details
EMPTY_LIST
public static final List EMPTY_LISTList.EMPTY_SET
public static final Set EMPTY_SETSet.EMPTY_MAP
public static final Map EMPTY_MAPMap.Method details
binarySearch
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T object)Parameters
listList<? extends Comparable<? super T>>- the sorted list to search.
objectT- the element to find.
Returns
-index - 1 where the element would be insertedThrows
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)Parameters
listList<? extends T>- the sorted List to search.
objectT- the element to find.
comparatorComparator<? super T>- the comparator. If the comparator is
nullthen the search uses the objects’ natural ordering.
Returns
-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 <T> void copy(List<? super T> destination, List<? extends T> source)index >= source.size() will be unchanged.Parameters
destinationList<? super T>- the list whose elements are set from the source list.
sourceList<? extends T>- 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
public static <T> Enumeration<T> enumeration(Collection<T> collection)Enumeration on the specified collection.Parameters
collectionCollection<T>- the collection to enumerate.
Returns
fill
public static <T> void fill(List<? super T> list, T object)Parameters
listList<? super T>- the list to fill.
objectT- the element to fill the list with.
Throws
UnsupportedOperationException- when replacing an element in the List is not supported.
max
public static <T extends Comparable<? super T>> T max(Collection<? extends T> collection)Parameters
collectionCollection<? extends T>- the collection to search.
Returns
Throws
ClassCastException- when an element in the collection does not implement
Comparableor elements cannot be compared to each other.
max
public static <T> T max(Collection<? extends T> collection, Comparator<? super T> comparator)Parameters
collectionCollection<? extends T>- the collection to search.
comparatorComparator<? super T>- the comparator.
Returns
Throws
ClassCastException- when elements in the collection cannot be compared to each
other using the
Comparator.
min
public static <T extends Comparable<? super T>> T min(Collection<? extends T> collection)Parameters
collectionCollection<? extends T>- the collection to search.
Returns
Throws
ClassCastException- when an element in the collection does not implement
Comparableor elements cannot be compared to each other.
min
public static <T> T min(Collection<? extends T> collection, Comparator<? super T> comparator)Parameters
collectionCollection<? extends T>- the collection to search.
comparatorComparator<? super T>- the comparator.
Returns
Throws
ClassCastException- when elements in the collection cannot be compared to each
other using the
Comparator.
nCopies
public static <T> List<T> nCopies(int length, T object)Parameters
lengthint- the size of the returned list.
objectT- the element to be added
lengthtimes to a list.
Returns
length copies of the element.Throws
IllegalArgumentException- when
length < 0.
reverse
public static void reverse(List<?> list)List by reversing the order of the
elements.Parameters
listList<?>- the list to reverse.
Throws
UnsupportedOperationException- when replacing an element in the List is not supported.
reverseOrder
public static <T> Comparator<T> reverseOrder()Comparator that’s returned is Serializable.Returns
Comparator instance.See also
reverseOrder
public static <T> Comparator<T> reverseOrder(Comparator<T> c)Returns a Comparator that reverses the order of the
Comparator passed. If the Comparator passed is
null, then this method is equivalent to #reverseOrder().
The Comparator that’s returned is Serializable if the
Comparator passed is serializable or null.
Parameters
cComparator<T>- the
Comparatorto reverse ornull.
Returns
Comparator instance.See also
shuffle
public static void shuffle(List<?> list)Parameters
listList<?>- 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)Parameters
listList<?>- the list to shuffle.
randomRandom- the random number generator.
Throws
UnsupportedOperationException- when replacing an element in the list is not supported.
singleton
public static <E> Set<E> singleton(E object)Parameters
objectE- the element.
Returns
singletonList
public static <E> List<E> singletonList(E object)Parameters
objectE- the element.
Returns
singletonMap
public static <K, V> Map<K, V> singletonMap(K key, V value)Parameters
keyK- the key.
valueV- the value.
Returns
sort
public static <T extends Comparable<? super T>> void sort(List<T> list)Parameters
listList<T>- 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 <T> void sort(List<T> list, Comparator<? super T> comparator)Parameters
listList<T>- the list to be sorted.
comparatorComparator<? super T>- 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)list at indices index1 and
index2.Parameters
listList<?>- the list to manipulate.
index1int- position of the first element to swap with the element in index2.
index2int- position of the other element.
Throws
IndexOutOfBoundsException- if index1 or index2 is out of range of this list.
replaceAll
public static <T> boolean replaceAll(List<T> list, T obj, T obj2)obj in list with
newObj. If the obj is null, then all
occurrences of null are replaced with newObj.Parameters
listList<T>- the list to modify.
objT- the object to find and replace occurrences of.
obj2T- the object to replace all occurrences of
objinlist.
Returns
obj has been found in
list.Throws
UnsupportedOperationException- if the list does not support setting elements.
rotate
public static void rotate(List<?> lst, int dist)Rotates the elements in list by the distance dist
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
lstList<?>- the list whose elements are to be rotated.
distint- is the distance the list is rotated. This can be any valid integer. Negative values rotate the list backwards.
indexOfSubList
public static int indexOfSubList(List<?> list, List<?> sublist)Searches the list for sublist and returns the beginning
index of the first occurrence.
-1 is returned if the sublist does not exist in list.
Parameters
listList<?>- the List to search
sublistin. sublistList<?>- the List to search in
list.
Returns
sublist in
list, or -1.lastIndexOfSubList
public static int lastIndexOfSubList(List<?> list, List<?> sublist)Searches the list for sublist and returns the beginning
index of the last occurrence.
-1 is returned if the sublist does not exist in list.
Parameters
listList<?>- the list to search
sublistin. sublistList<?>- the list to search in
list.
Returns
sublist in
list, or -1.list
public static <T> ArrayList<T> list(Enumeration<T> enumeration)ArrayList with all the elements in the enumeration. The elements in the returned ArrayList are in the
same order as in the enumeration.Parameters
enumerationEnumeration<T>- the source
Enumeration.
Returns
ArrayList from enumeration.synchronizedCollection
public static <T> Collection<T> synchronizedCollection(Collection<T> collection)Parameters
collectionCollection<T>- the Collection to wrap in a synchronized collection.
Returns
synchronizedList
public static <T> List<T> synchronizedList(List<T> list)Parameters
listList<T>- the List to wrap in a synchronized list.
Returns
synchronizedMap
public static <K, V> Map<K, V> synchronizedMap(Map<K, V> map)Parameters
mapMap<K, V>- the map to wrap in a synchronized map.
Returns
synchronizedSet
public static <E> Set<E> synchronizedSet(Set<E> set)Parameters
setSet<E>- the set to wrap in a synchronized set.
Returns
synchronizedSortedMap
public static <K, V> SortedMap<K, V> synchronizedSortedMap(SortedMap<K, V> map)Parameters
mapSortedMap<K, V>- the sorted map to wrap in a synchronized sorted map.
Returns
synchronizedSortedSet
public static <E> SortedSet<E> synchronizedSortedSet(SortedSet<E> set)Parameters
setSortedSet<E>- the sorted set to wrap in a synchronized sorted set.
Returns
unmodifiableCollection
public static <E> Collection<E> unmodifiableCollection(Collection<? extends E> collection)UnsupportedOperationException whenever an attempt is made to
modify the collection.Parameters
collectionCollection<? extends E>- the collection to wrap in an unmodifiable collection.
Returns
unmodifiableList
public static <E> List<E> unmodifiableList(List<? extends E> list)UnsupportedOperationException whenever an attempt is made to
modify the list.Parameters
listList<? extends E>- the list to wrap in an unmodifiable list.
Returns
unmodifiableMap
public static <K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> map)UnsupportedOperationException whenever an attempt is made to
modify the map.Parameters
mapMap<? extends K, ? extends V>- the map to wrap in an unmodifiable map.
Returns
unmodifiableSet
public static <E> Set<E> unmodifiableSet(Set<? extends E> set)UnsupportedOperationException whenever an attempt is made to
modify the set.Parameters
setSet<? extends E>- the set to wrap in an unmodifiable set.
Returns
unmodifiableSortedMap
public static <K, V> SortedMap<K, V> unmodifiableSortedMap(SortedMap<K, ? extends V> map)UnsupportedOperationException whenever an attempt is made to
modify the sorted map.Parameters
mapSortedMap<K, ? extends V>- the sorted map to wrap in an unmodifiable sorted map.
Returns
unmodifiableSortedSet
public static <E> SortedSet<E> unmodifiableSortedSet(SortedSet<E> set)UnsupportedOperationException whenever an attempt is made to
modify the sorted set.Parameters
setSortedSet<E>- the sorted set to wrap in an unmodifiable sorted set.
Returns
frequency
public static int frequency(Collection<?> c, Object o)Collection that match the
Object passed. If the Object is null, then the
number of null elements is returned.Parameters
cCollection<?>- the
Collectionto search. oObject- the
Objectto search for.
Returns
Throws
NullPointerException- if the
Collectionparameter isnull.
emptyList
public static final <T> List<T> emptyList()List.Returns
List.See also
emptySet
public static final <T> Set<T> emptySet()Set.Returns
Set.See also
emptyMap
public static final <K, V> Map<K, V> emptyMap()Map.Returns
Map.See also
checkedCollection
public static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type)ClassCastException. At creation time the types in c are
not checked for correct type.Parameters
cCollection<E>- the collection to be wrapped in a typesafe collection.
typeClass<E>- the type of the elements permitted to insert.
Returns
addAll
public static <T> boolean addAll(Collection<? super T> c, T... a)Parameters
cCollection<? super T>- the collection the elements are to be inserted into.
aT...- the elements to insert.
Returns
Throws
UnsupportedOperationException- when the method is not supported.
NullPointerException- when
coraisnull, oracontains one or morenullelements andcdoesn’t supportnullelements. IllegalArgumentException- if at least one of the elements can’t be inserted into the collection.
disjoint
public static boolean disjoint(Collection<?> c1, Collection<?> c2)Parameters
c1Collection<?>- the first collection.
c2Collection<?>- the second collection.
Returns
true if the collections have no elements in common,
false otherwise.Throws
NullPointerException- if one of the collections is
null.
newSetFromMap
public static <E> Set<E> newSetFromMap(Map<E, Boolean> map)Answers a set backed by a map. And the map must be empty when this method is called.
Type parameter E: type of elements in set
Parameters
mapMap<E, Boolean>- the backing map
Returns
Throws
IllegalArgumentException- if the map is not empty
asLifoQueue
public static <T> Queue<T> asLifoQueue(Deque<T> deque)Answers a LIFO Queue as a view of a Deque. Methods in the returned Queue need to be re-written to implement the LIFO feature.
Type parameter T: type of elements
Parameters
dequeDeque<T>- the Deque