Facebook Page Twitter Page LinkedIn Page
× Java Utility Library


Java Collections Class

Java.util package provides a Collections class which consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection. The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

Class declaration

The declaration of java.util.Collections class is:

public class Collections
  extends Object

Fields

Following are the fields for java.util.Collections class:

S.NField & Description
1. static List EMPTY_LIST
The empty list (immutable).
2. static Map EMPTY_MAP
The empty map (immutable).
3. static Set EMPTY_SET
The empty set (immutable).

java.util.Collections Methods

The java.util.Collections class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. static <T> boolean addAll(Collection<? super T> c, T... elements)
Adds all of the specified elements to the specified collection.
2. static <T> Queue<T> asLifoQueue(Deque<T> deque)
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)
Searches the specified list for the specified object using the binary search algorithm.
4. 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.
5. static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type)
Returns a dynamically typesafe view of the specified collection.
6. static <E> List<E> checkedList(List<E> list, Class<E> type)
Returns a dynamically typesafe view of the specified list.
7. 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.
8. 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.
9. static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s, Class<E> type)
Returns a dynamically typesafe view of the specified navigable set.
10. static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type)
Returns a dynamically typesafe view of the specified queue.
11. static <E> Set<E> checkedSet(Set<E> s, Class<E> type)
Returns a dynamically typesafe view of the specified set.
12. 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.
13. static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s, Class<E> type)
Returns a dynamically typesafe view of the specified sorted set.
14. static <T> void copy(List<? super T> dest, List<? extends T> src)
Copies all of the elements from one list into another.
15. static boolean disjoint(Collection<?> c1, Collection<?> c2)
Returns true if the two specified collections have no elements in common.
16. static <T> Enumeration<T> emptyEnumeration()
Returns an enumeration that has no elements.
17. static <T> Iterator<T> emptyIterator()
Returns an iterator that has no elements.
18. static <T> List<T> emptyList()
Returns an empty list (immutable).
19. static <T> ListIterator<T> emptyListIterator()
Returns a list iterator that has no elements.
20. static <K,V> Map<K,V> emptyMap()
Returns an empty map (immutable).
21. static <K,V> NavigableMap<K,V> emptyNavigableMap()
Returns an empty navigable map (immutable).
22. static <E> NavigableSet<E> emptyNavigableSet()
Returns an empty navigable set (immutable).
23. static <T> Set<T> emptySet()
Returns an empty set (immutable).
24. static <K,V> SortedMap<K,V> emptySortedMap()
Returns an empty sorted map (immutable).
25. static <E> SortedSet<E> emptySortedSet()
Returns an empty sorted set (immutable).
26. static <T> Enumeration<T> enumeration(Collection<T> c)
Returns an enumeration over the specified collection.
27. static <T> void fill(List<? super T> list, T obj)
Replaces all of the elements of the specified list with the specified element.
28. static int frequency(Collection<?> c, Object o)
Returns the number of elements in the specified collection equal to the specified object.
29. 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.
30. 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.
31. 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.
32. 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.
33. 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.
34. 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.
35. 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.
36. static <T> List<T> nCopies(int n, T o)
Returns an immutable list consisting of n copies of the specified object.
37. static <E> Set<E> newSetFromMap(Map<E,Boolean> map)
Returns a set backed by the specified map.
38. static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
Replaces all occurrences of one specified value in a list with another.
39. static void reverse(List<?> list)
Reverses the order of the elements in the specified list.
40. 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.
41. static <T> Comparator<T> reverseOrder(Comparator<T> cmp)
Returns a comparator that imposes the reverse ordering of the specified comparator.
42. static void rotate(List<?> list, int distance)
Rotates the elements in the specified list by the specified distance.
43. static void shuffle(List<?> list)
Randomly permutes the specified list using a default source of randomness.
44. static void shuffle(List<?> list, Random rnd)
Randomly permute the specified list using the specified source of randomness.
45. static <T> Set<T> singleton(T o)
Returns an immutable set containing only the specified object.
46. static <T> List<T> singletonList(T o)
Returns an immutable list containing only the specified object.
47. static <K,V> Map<K,V> singletonMap(K key, V value)
Returns an immutable map, mapping only the specified key to the specified value.
48. 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.
49. static <T> void sort(List<T> list, Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
50. static void swap(List<?> list, int i, int j)
Swaps the elements at the specified positions in the specified list.
51. static <T> Collection<T> synchronizedCollection(Collection<T> c)
Returns a synchronized (thread-safe) collection backed by the specified collection.
52. static <T> List<T> synchronizedList(List<T> list)
Returns a synchronized (thread-safe) list backed by the specified list.
53. static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
Returns a synchronized (thread-safe) map backed by the specified map.
54. static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m)
Returns a synchronized (thread-safe) navigable map backed by the specified navigable map.
55. static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s)
Returns a synchronized (thread-safe) navigable set backed by the specified navigable set.
56. static <T> Set<T> synchronizedSet(Set<T> s)
Returns a synchronized (thread-safe) set backed by the specified set.
57. static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)
Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.
58. static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)
Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.
59. static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
Returns an unmodifiable view of the specified collection.
60. static <T> List<T> unmodifiableList(List<? extends T> list)
Returns an unmodifiable view of the specified list.
61. static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
Returns an unmodifiable view of the specified map.
62. static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K,? extends V> m)
Returns an unmodifiable view of the specified navigable map.
63. static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s)
Returns an unmodifiable view of the specified navigable set.
64. static <T> Set<T> unmodifiableSet(Set<? extends T> s)
Returns an unmodifiable view of the specified set.
65. static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)
Returns an unmodifiable view of the specified sorted map.
66. static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
Returns an unmodifiable view of the specified sorted set.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object

5