[Link].
COLLECTIONS CLASS
[Link] rialspo [Link] m/java/util/java_util_co lle ctio [Link] Co pyrig ht © tuto rials po [Link] m
Introduction
T he [Link] tions class consists exclusively of static methods that operate on or return
[Link] are the important points about Collections:
It contains polymorphic alg orithms that operate on collections, "wrappers", which return a new collection
backed by a specified collection.
T he methods of this class all throw a NullPointerException if the collections or class objects provided to
them are null.
Class declaration
Following is the declaration for [Link] tions class:
public class Collections
extends Object
Field
Following are the fields for [Link] tions class:
static List EMPT Y_LIST -- T his is the empty list (immutable).
static Map EMPT Y_MAP -- T his is the empty map (immutable).
static Set EMPT Y_SET -- T his is the empty set (immutable).
Class methods
S.N. Method & Desc ription
1 static <T > boolean addAll(Collection<? super T > c, T ... elements)
T his method adds all of the specified elements to the specified collection.
2 static <T > Queue<T > asLifoQueue(Deque<T > deque)
T his method returns a view of a Deque as a Last-in-first-out (Lifo) Queue.
3 static <T > int binarySearch(List<? extends Comparable<? super T >> list, T key)
T his method searches the specified list for the specified object using the binary search alg orithm.
4 static <T > int binarySearch(List<? extends T > list, T key, Comparator<? super T < c)
T his method searches the specified list for the specified object using the binary search alg orithm.
5 static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type)
T his method returns a dynamically typesafe view of the specified collection.
6 static <E> List<E> checkedList(List<E> list, Class<E> type)
T his method returns a dynamically typesafe view of the specified list.
7 static <K,V> Map<K,V> checkedMap(Map<K,V> m, Class<K> keyT ype, Class<V> valueT ype)
T his method returns a dynamically typesafe view of the specified map.
8 static <E> Set<E> checkedSet(Set<E> s, Class<E> type)
T his method returns a dynamically typesafe view of the specified set.
9 static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m, Class<K> keyT ype,
Class<V> valueT ype)
T his method returns a dynamically typesafe view of the specified sorted map.
10 static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s, Class<E> type)
T his method returns a dynamically typesafe view of the specified sorted set.
11 static <T > void copy(List<? super T > dest, List<? extends T > src)
T his method copies all of the elements from one list into another.
12 static boolean disjoint(Collection<?> c1, Collection<?> c2)
T his method returns true if the two specified collections have no elements in common.
13 static <T > List<T > emptyList()
T his method returns the empty list (immutable).
14 static <K,V> Map<K,V> emptyMap()
T his method returns the empty map (immutable).
15 static <T > Set<T > emptySet()
T his method returns the empty set (immutable).
16 static <T > Enumeration<T > enumeration(Collection<T > c)
T his method returns an enumeration over the specified collection.
17 static <T > void fill(List<? super T > list, T obj)
T his method replaces all of the elements of the specified list with the specified element.
18 static int frequency(Collection<?> c, Object o)
T his method returns the number of elements in the specified collection equal to the specified object.
19 static int indexOfSubList(List<?> source, List<?> targ et)
T his method returns the starting position of the first occurrence of the specified targ et list within the
specified source list, or -1 if there is no such occurrence.
20 static int lastIndexOfSubList(List<?> source, List<?> targ et)
T his method returns the starting position of the last occurrence of the specified targ et list within the
specified source list, or -1 if there is no such occurrence.
21 static <T > ArrayList<T > list(Enumeration<T > e)
T his method returns an array list containing the elements returned by the specified enumeration in the
order they are returned by the enumeration.
22 static <T extends Object & Comparable<? super T > >T max(Collection<? extends T > coll)
T his method returns the maximum element of the g iven collection, according to the natural ordering
of its elements.
23 static <T > T max(Collection<? extends T > coll, Comparator<? super T > comp)
T his method returns the maximum element of the g iven collection, according to the order induced by
the specified comparator.
24 static <T extends Object & Comparable<? super T >>T min(Collection<? extends T > coll)
T his method Returns the minimum element of the g iven collection, according to the natural ordering
of its elements.
25 static <T > T min(Collection<? extends T > coll, Comparator<? super T > comp)
T his method returns the minimum element of the g iven collection, according to the order induced by
the specified comparator.
26 static <T > List<T > nCopies(int n, T o)
T his method returns an immutable list consisting of n copies of the specified object.
27 static <E> Set<E> newSetFromMap(Map<E,Boolean> map)
T his method returns a set backed by the specified map.
28 static <T > boolean replaceAll(List<T > list, T oldVal, T newVal)
T his method replaces all occurrences of one specified value in a list with another.
29 static void reverse(List<?> list)
T his method reverses the order of the elements in the specified list
30 static <T > Comparator<T > reverseOrder()
T his method returns a comparator that imposes the reverse of the natural ordering on a collection of
objects that implement the Comparable interface.
31 static <T > Comparator<T > reverseOrder(Comparator<T > cmp)
T his method returns a comparator that imposes the reverse ordering of the specified comparator.
32 static void rotate(List<?> list, int distance)
T his method rotates the elements in the specified list by the specified distance.
33 static void shuffle(List<?> list)
T his method randomly permutes the specified list using a default source of randomness.
34 static void shuffle(List<?> list, Random rnd)
T his method randomly permute the specified list using the specified source of randomness.
35 static <T > Set<T > sing leton(T o)
T his method returns an immutable set containing only the specified object.
36 static <T > List<T > sing letonList(T o)
T his method returns an immutable list containing only the specified object.
37 static <K,V> Map<K,V> sing letonMap(K key, V value)
T his method returns an immutable map, mapping only the specified key to the specified value.
38 static <T extends Comparable<? super T >> void sort(List<T > list)
T his method sorts the specified list into ascending order, according to the natural ordering of its
elements.
39 static <T > void sort(List<T > list, Comparator<? super T > c)
T his method sorts the specified list according to the order induced by the specified comparator.
40 static void swap(List<?> list, int i, int j)
T his method swaps the elements at the specified positions in the specified list.
41 static <T > Collection<T > synchronizedCollection(Collection<T > c)
T his method returns a synchronized (thread-safe) collection backed by the specified collection.
42 static <T > List<T > synchronizedList(List<T > list)
T his method returns a synchronized (thread-safe) list backed by the specified list.
43 static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
T his method returns a synchronized (thread-safe) map backed by the specified map.
44 static <T > Set<T > synchronizedSet(Set<T > s)
T his method returns a synchronized (thread-safe) set backed by the specified set.
45 static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)
T his method returns a synchronized (thread-safe) sorted map backed by the specified sorted map.
46 static <T > SortedSet<T > synchronizedSortedSet(SortedSet<T > s)
T his method returns a synchronized (thread-safe) sorted set backed by the specified sorted set.
47 static <T > Collection<T > unmodifiableCollection(Collection<? extends T > c)
T his method returns an unmodifiable view of the specified collection.
48 static <T > List<T > unmodifiableList(List<? extends T > list)
T his method returns an unmodifiable view of the specified list.
49 static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
T his method returns an unmodifiable view of the specified map.
50 static <T > Set<T > unmodifiableSet(Set<? extends T > s)
T his method returns an unmodifiable view of the specified set.
51 static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)
T his method returns an unmodifiable view of the specified sorted map
52 static <T > SortedSet<T > unmodifiableSortedSet(SortedSet<T > s)
T his method returns an unmodifiable view of the specified sorted set.
Methods inherited
T his class inherits methods from the following classes:
[Link]