public class Collections extends Object
Collections
contains static methods which operate on
Collection
classes.Modifier and Type | Class and Description |
---|---|
static class |
Collections.UnmodEntry
UnmodEntry:
|
Modifier and Type | Field and Description |
---|---|
static List |
EMPTY_LIST
An empty immutable instance of
List . |
static Map |
EMPTY_MAP
An empty immutable instance of
Map . |
static Set |
EMPTY_SET
An empty immutable instance of
Set . |
Modifier and Type | Method and Description |
---|---|
static int |
binarySearch(List list,
Object object)
Performs a binary search for the specified element in the specified
sorted list.
|
static int |
binarySearch(List list,
Object object,
Comparator comparator)
Performs a binary search for the specified element in the specified
sorted list using the specified comparator.
|
static void |
copy(List destination,
List source)
Copies the elements from the source list to the destination list.
|
static Enumeration |
enumeration(Collection collection)
Returns an
Enumeration on the specified collection. |
static void |
fill(List list,
Object object)
Fills the specified list with the specified element.
|
static int |
indexOfSubList(List list,
List sublist)
Searches the
list for sublist and returns the beginning
index of the first occurrence. |
static int |
lastIndexOfSubList(List list,
List sublist)
Searches the
list for sublist and returns the beginning
index of the last occurrence. |
static ArrayList |
list(Enumeration enumeration)
Returns an
ArrayList with all the elements in the enumeration . |
static Object |
max(Collection collection)
Searches the specified collection for the maximum element.
|
static Object |
max(Collection collection,
Comparator comparator)
Searches the specified collection for the maximum element using the
specified comparator.
|
static Object |
min(Collection collection)
Searches the specified collection for the minimum element.
|
static Object |
min(Collection collection,
Comparator comparator)
Searches the specified collection for the minimum element using the
specified comparator.
|
static List |
nCopies(int length,
Object object)
Returns a list containing the specified number of the specified element.
|
static boolean |
replaceAll(List list,
Object obj,
Object obj2)
Replaces all occurrences of Object
obj in list with
newObj . |
static void |
reverse(List list)
Modifies the specified
List by reversing the order of the
elements. |
static Comparator |
reverseOrder()
A comparator which reverses the natural order of the elements.
|
static Comparator |
reverseOrder(Comparator c)
Returns a
Comparator that reverses the order of the
Comparator passed. |
static void |
rotate(List lst,
int dist)
Rotates the elements in
list by the distance dist |
static void |
shuffle(List list)
Moves every element of the list to a random new position in the list.
|
static void |
shuffle(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 Set |
singleton(Object object)
Returns a set containing the specified element.
|
static List |
singletonList(Object object)
Returns a list containing the specified element.
|
static Map |
singletonMap(Object key,
Object value)
Returns a Map containing the specified key and value.
|
static void |
sort(List list)
Sorts the specified list in ascending natural order.
|
static void |
sort(List list,
Comparator comparator)
Sorts the specified list using the specified comparator.
|
static void |
swap(List list,
int index1,
int index2)
Swaps the elements of list
list at indices index1 and
index2 . |
static Collection |
synchronizedCollection(Collection collection)
Returns a wrapper on the specified collection which synchronizes all
access to the collection.
|
static List |
synchronizedList(List list)
Returns a wrapper on the specified List which synchronizes all access to
the List.
|
static Map |
synchronizedMap(Map map)
Returns a wrapper on the specified map which synchronizes all access to
the map.
|
static Set |
synchronizedSet(Set set)
Returns a wrapper on the specified set which synchronizes all access to
the set.
|
static SortedMap |
synchronizedSortedMap(SortedMap map)
Returns a wrapper on the specified sorted map which synchronizes all
access to the sorted map.
|
static SortedSet |
synchronizedSortedSet(SortedSet set)
Returns a wrapper on the specified sorted set which synchronizes all
access to the sorted set.
|
static Collection |
unmodifiableCollection(Collection collection)
Returns a wrapper on the specified collection which throws an
UnsupportedOperationException whenever an attempt is made to
modify the collection. |
static List |
unmodifiableList(List list)
Returns a wrapper on the specified list which throws an
UnsupportedOperationException whenever an attempt is made to
modify the list. |
static Map |
unmodifiableMap(Map map)
Returns a wrapper on the specified map which throws an
UnsupportedOperationException whenever an attempt is made to
modify the map. |
static Set |
unmodifiableSet(Set set)
Returns a wrapper on the specified set which throws an
UnsupportedOperationException whenever an attempt is made to
modify the set. |
static SortedMap |
unmodifiableSortedMap(SortedMap map)
Returns a wrapper on the specified sorted map which throws an
UnsupportedOperationException whenever an attempt is made to
modify the sorted map. |
static SortedSet |
unmodifiableSortedSet(SortedSet set)
Returns a wrapper on the specified sorted set which throws an
UnsupportedOperationException whenever an attempt is made to
modify the sorted set. |
public static int binarySearch(List list, Object object)
list
- the sorted list to search.object
- the element to find.-index - 1
where the element would be insertedClassCastException
- if an element in the List or the search element does not
implement Comparable, or cannot be compared to each other.public static int binarySearch(List list, Object object, Comparator comparator)
T
- The element typelist
- the sorted List to search.object
- the element to find.comparator
- the comparator. If the comparator is null
then the
search uses the objects' natural ordering.-index - 1
where the element would be inserted.ClassCastException
- when an element in the list and the searched element cannot
be compared to each other using the comparator.public static void copy(List destination, List source)
index >= source.size()
will be unchanged.destination
- the list whose elements are set from the source list.source
- the list with the elements to be copied into the destination.IndexOutOfBoundsException
- when the destination list is smaller than the source list.UnsupportedOperationException
- when replacing an element in the destination list is not
supported.public static Enumeration enumeration(Collection collection)
Enumeration
on the specified collection.collection
- the collection to enumerate.public static void fill(List list, Object object)
list
- the list to fill.object
- the element to fill the list with.UnsupportedOperationException
- when replacing an element in the List is not supported.public static Object max(Collection collection)
collection
- the collection to search.ClassCastException
- when an element in the collection does not implement
Comparable
or elements cannot be compared to each
other.public static Object max(Collection collection, Comparator comparator)
collection
- the collection to search.comparator
- the comparator.ClassCastException
- when elements in the collection cannot be compared to each
other using the Comparator
.public static Object min(Collection collection)
collection
- the collection to search.ClassCastException
- when an element in the collection does not implement
Comparable
or elements cannot be compared to each
other.public static Object min(Collection collection, Comparator comparator)
collection
- the collection to search.comparator
- the comparator.ClassCastException
- when elements in the collection cannot be compared to each
other using the Comparator
.public static List nCopies(int length, Object object)
length
- the size of the returned list.object
- the element to be added length
times to a list.length
copies of the element.IllegalArgumentException
- when length < 0
.public static void reverse(List list)
List
by reversing the order of the
elements.list
- the list to reverse.UnsupportedOperationException
- when replacing an element in the List is not supported.public static Comparator reverseOrder()
Comparator
that's returned is Serializable
.Comparator
instance.Comparator
,
Comparable
,
Serializable
public static Comparator reverseOrder(Comparator c)
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
.
c
- the Comparator
to reverse or null
.Comparator
instance.Comparator
public static void shuffle(List list)
list
- the List to shuffle.UnsupportedOperationException
- when replacing an element in the List is not supported.public static void shuffle(List list, Random random)
list
- the list to shuffle.random
- the random number generator.UnsupportedOperationException
- when replacing an element in the list is not supported.public static Set singleton(Object object)
object
- the element.public static List singletonList(Object object)
object
- the element.public static Map singletonMap(Object key, Object value)
key
- the key.value
- the value.public static void sort(List list)
list
- the list to be sorted.ClassCastException
- when an element in the List does not implement Comparable or
elements cannot be compared to each other.public static void sort(List list, Comparator comparator)
list
- the list to be sorted.comparator
- the comparator.ClassCastException
- when elements in the list cannot be compared to each other
using the comparator.public static void swap(List list, int index1, int index2)
list
at indices index1
and
index2
.list
- the list to manipulate.index1
- position of the first element to swap with the element in
index2.index2
- position of the other element.IndexOutOfBoundsException
- if index1 or index2 is out of range of this list.public static boolean replaceAll(List list, Object obj, Object obj2)
obj
in list
with
newObj
. If the obj
is null
, then all
occurrences of null
are replaced with newObj
.list
- the list to modify.obj
- the object to find and replace occurrences of.obj2
- the object to replace all occurrences of obj
in
list
.obj
has been found in
list
.UnsupportedOperationException
- if the list does not support setting elements.public static void rotate(List lst, int dist)
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]
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.public static int indexOfSubList(List list, List sublist)
list
for sublist
and returns the beginning
index of the first occurrence.
-1 is returned if the sublist
does not exist in list
.
list
- the List to search sublist
in.sublist
- the List to search in list
.sublist
in
list
, or -1.public static int lastIndexOfSubList(List list, List sublist)
list
for sublist
and returns the beginning
index of the last occurrence.
-1 is returned if the sublist
does not exist in list
.
list
- the list to search sublist
in.sublist
- the list to search in list
.sublist
in
list
, or -1.public static Collection synchronizedCollection(Collection collection)
collection
- the Collection to wrap in a synchronized collection.public static List synchronizedList(List list)
list
- the List to wrap in a synchronized list.public static Map synchronizedMap(Map map)
map
- the map to wrap in a synchronized map.public static Set synchronizedSet(Set set)
set
- the set to wrap in a synchronized set.public static SortedMap synchronizedSortedMap(SortedMap map)
map
- the sorted map to wrap in a synchronized sorted map.public static SortedSet synchronizedSortedSet(SortedSet set)
set
- the sorted set to wrap in a synchronized sorted set.public static Collection unmodifiableCollection(Collection collection)
UnsupportedOperationException
whenever an attempt is made to
modify the collection.collection
- the collection to wrap in an unmodifiable collection.public static List unmodifiableList(List list)
UnsupportedOperationException
whenever an attempt is made to
modify the list.list
- the list to wrap in an unmodifiable list.public static Map unmodifiableMap(Map map)
UnsupportedOperationException
whenever an attempt is made to
modify the map.map
- the map to wrap in an unmodifiable map.public static Set unmodifiableSet(Set set)
UnsupportedOperationException
whenever an attempt is made to
modify the set.set
- the set to wrap in an unmodifiable set.public static SortedMap unmodifiableSortedMap(SortedMap map)
UnsupportedOperationException
whenever an attempt is made to
modify the sorted map.map
- the sorted map to wrap in an unmodifiable sorted map.public static SortedSet unmodifiableSortedSet(SortedSet set)
UnsupportedOperationException
whenever an attempt is made to
modify the sorted set.set
- the sorted set to wrap in an unmodifiable sorted set.public static ArrayList list(Enumeration enumeration)
ArrayList
with all the elements in the enumeration
. The elements in the returned ArrayList
are in the
same order as in the enumeration
.enumeration
- the source Enumeration
.ArrayList
from enumeration
.