Java Collections Class | Java Collections Class Methods

Last Updated : 6 Jan 2026

The Java Collections class belongs to java.util package and provides static utility methods that operate on return collections, such as List, Set, and Map. These methods are mostly used to perform common operations such as sorting, searching, reversing and synchronization.

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

To read more Collections in Java

Java Collections Class | Java Collections Class Methods

Java Collections Class Methods

Modifier & TypeMethodsDescriptions
static <T> booleanaddAll()It is used to adds all of the specified elements to the specified collection.
static <T> Queue<T>asLifoQueue()It returns a view of a Deque as a Last-in-first-out (LIFO) Queue.
static <T> intbinarySearch()It searches the list for the specified object and returns their position in a sorted list.
static <E> Collection<E>checkedCollection()It is used to returns a dynamically typesafe view of the specified collection.
static <E> List<E>checkedList()It is used to returns a dynamically typesafe view of the specified list.
static <K,V> Map<K,V>checkedMap()It is used to returns a dynamically typesafe view of the specified map.
static <K,V> NavigableMap<K,V>checkedNavigableMap()It is used to returns a dynamically typesafe view of the specified navigable map.
static <E> NavigableSet<E>checkedNavigableSet()It is used to returns a dynamically typesafe view of the specified navigable set.
static <E> Queue<E>checkedQueue()It is used to returns a dynamically typesafe view of the specified queue.
static <E> Set<E>checkedSet()It is used to returns a dynamically typesafe view of the specified set.
static <K,V> SortedMap<K,V>checkedSortedMap()It is used to returns a dynamically typesafe view of the specified sorted map.
static <E> SortedSet<E>checkedSortedSet()It is used to returns a dynamically typesafe view of the specified sorted set.
static <T> voidcopy()It is used to copy all the elements from one list into another list.
static booleandisjoint()It returns true if the two specified collections have no elements in common.
static <T> Enumeration<T>emptyEnumeration()It is used to get an enumeration that has no elements.
static <T> Iterator<T>emptyIterator()It is used to get an Iterator that has no elements.
static <T> List<T>emptyList()It is used to get a List that has no elements.
static <T> ListIterator<T>emptyListIterator()It is used to get a List Iterator that has no elements.
static <K,V> Map<K,V>emptyMap()It returns an empty map which is immutable.
static <K,V> NavigableMap<K,V>emptyNavigableMap()It returns an empty navigable map which is immutable.
static <E> NavigableSet<E>emptyNavigableSet()It is used to get an empty navigable set which is immutable in nature.
static <T> Set<T>emptySet()It is used to get the set that has no elements.
static <K,V> SortedMap<K,V>emptySortedMap()It returns an empty sorted map which is immutable.
static <E> SortedSet<E>emptySortedSet()It is used to get the sorted set that has no elements.
static <T> Enumeration<T>enumeration()It is used to get the enumeration over the specified collection.
static <T> voidfill()It is used to replace all of the elements of the specified list with the specified elements.
static intfrequency()It is used to get the number of elements in the specified collection equal to the specified object.
static intindexOfSubList()It is used to get the starting position of the first occurrence of the specified target list within the specified source list. It returns -1 if there is no such occurrence in the specified list.
static intlastIndexOfSubList()It is used to get the starting position of the last occurrence of the specified target list within the specified source list. It returns -1 if there is no such occurrence in the specified list.
static <T> ArrayList<T>list()It is used to get an array list containing the elements returned by the specified enumeration in the order in which they are returned by the enumeration.
static <T extends Object & Comparable<? super T>> Tmax()It is used to get the maximum value of the given collection, according to the natural ordering of its elements.
static <T extends Object & Comparable<? super T>> Tmin()It is used to get the minimum value of the given collection, according to the natural ordering of its elements.
static <T> List<T>nCopies()It is used to get an immutable list consisting of n copies of the specified object.
static <E> Set<E>newSetFromMap()It is used to return a set backed by the specified map.
static <T> booleanreplaceAll()It is used to replace all occurrences of one specified value in a list with the other specified value.
static voidreverse()It is used to reverse the order of the elements in the specified list.
static <T> Comparator<T>reverseOrder()It is used to get the comparator that imposes the reverse of the natural ordering on a collection of objects which implement the Comparable interface.
static voidrotate()It is used to rotate the elements in the specified list by a given distance.
static voidshuffle()It is used to randomly reorders the specified list elements using a default randomness.
static <T> Set<T>singleton()It is used to get an immutable set which contains only the specified object.
static <T> List<T>singletonList()It is used to get an immutable list which contains only the specified object.
static <K,V> Map<K,V>singletonMap()It is used to get an immutable map, mapping only the specified key to the specified value.
static <T extends Comparable<? super T>>voidsort()It is used to sort the elements presents in the specified list of collection in ascending order.
static voidswap()It is used to swap the elements at the specified positions in the specified list.
static <T> Collection<T>synchronizedCollection()It is used to get a synchronized (thread-safe) collection backed by the specified collection.
static <T> List<T>synchronizedList()It is used to get a synchronized (thread-safe) collection backed by the specified list.
static <K,V> Map<K,V>synchronizedMap()It is used to get a synchronized (thread-safe) map backed by the specified map.
static <K,V> NavigableMap<K,V>synchronizedNavigableMap()It is used to get a synchronized (thread-safe) navigable map backed by the specified navigable map.
static <T> NavigableSet<T>synchronizedNavigableSet()It is used to get a synchronized (thread-safe) navigable set backed by the specified navigable set.
static <T> Set<T>synchronizedSet()It is used to get a synchronized (thread-safe) set backed by the specified set.
static <K,V> SortedMap<K,V>synchronizedSortedMap()It is used to get a synchronized (thread-safe) sorted map backed by the specified sorted map.
static <T> SortedSet<T>synchronizedSortedSet()It is used to get a synchronized (thread-safe) sorted set backed by the specified sorted set.
static <T> Collection<T>unmodifiableCollection()It is used to get an unmodifiable view of the specified collection.
static <T> List<T>unmodifiableList()It is used to get an unmodifiable view of the specified list.
static <K,V> Map<K,V>unmodifiableMap()It is used to get an unmodifiable view of the specified map.
static <K,V> NavigableMap<K,V>unmodifiableNavigableMap()It is used to get an unmodifiable view of the specified navigable map.
static <T> NavigableSet<T>unmodifiableNavigableSet()It is used to get an unmodifiable view of the specified navigable set.
static <T> Set<T>unmodifiableSet()It is used to get an unmodifiable view of the specified set.
static <K,V> SortedMap<K,V>unmodifiableSortedMap()It is used to get an unmodifiable view of the specified sorted map.
static <T> SortedSet<T>unmodifiableSortedSet()It is used to get an unmodifiable view of the specified sorted set.

Let's use some of the above methods in Java program.

Java Collections Class Method Examples

Collections.addAll() Method

The method adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array.

Syntax:

Example

Compile and Run

Output:

Initial collection value:[C, Core Java, Advance Java]
After adding elements collection value:[C, Core Java, Advance Java, Servlet, JSP]
After adding array collection value:[C, Core Java, Advance Java, Servlet, JSP, C#, .Net]

Collections.max() Method

This method returns the maximum element from the given collection.

Syntax:

Example

Compile and Run

Output:

Value of maximum element from the collection: 67

Explanation

In the above code, we have a list of numbers [46, 67, 24, 16, 8, 12]. The collection.max() method is used to find the largest number in the list which is 67. It works based on the natural order of integers. The method then prints the minimum value. It is the easiest way to find the biggest item in any collection.

Collections.min() Method

This method returns the minimum element from the given collection.

Syntax:

Example

Compile and Run

Output:

Value of minimum element from the collection: 8

Explanation

In the above code, we have a list of numbers [46, 67, 24, 16, 8, 12]. The collection.min() method finds the smallest number in the list which is 8. It works based on the natural order of integers. The method then prints the minimum value. It is the easiest way to find the smallest item in any collection.

Collections.sort() Method

The methos is used to sort the given elements. If the elements do not implement Comparable, this method throws a ClassCastException.

Example

Compile and Run

Output:

[5, 7, 11, 14]

Explanation

In the above code, we create a list of integers (14, 5, 11, 7) using Arrays.asList(). The Collections.sort() method is used to sort the list in ascending order based on their natural order (i.e., from smallest to largest). After sorting, the System.out.println() method displays the sorted list: [5, 7, 11, 14].

Collections.reverse() Method

The method modifies the original list directly which makes it simple way to reverse collection without the need of custom code.

Syntax:

Example

Compile and Run

Output:

[Kiwi, Pineapple, Banana]

Explanation

In the above code, we have created a list of fruits ("Banana", "Pineapple", "Kiwi") using Arrays.asList() and wrap it with an ArrayList. The Collections.reverse() method is then called to reverse the order of elements in the list.

This method modifies the original list directly, so the order changes from [Banana, Pineapple, Kiwi] to [Kiwi, Pineapple, Banana]. Hence, the reversed list is printed to console. This method is simple and efficient to make it easy to reverse collections.

Collections.shuffle() Method

This method randomly reorders the elements in the given list using default randomness.

Syntax:

Example

Compile and Run

Output:

[8, 7, 5, 9, 6]

Explanation

In the above code we have created a list of integers (5, 6, 7, 8, 9) using ArrayList() and wrap it with an ArrayList. The collection.shuffle() method is then used to randomly shuffle the elements in the list, introducing randomness to their order.

After shuffling, the list might appear as [8, 7, 5, 9, 6] the order will change each time when we run the code. This method is helpful in scenarios like card games or any case where we need to rearrange a collection of items randomly.

Collections.swap() Method

Swaps the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)

Syntax:

Parameters:

list: The list in which to swap elements.

i: the index of one element to be swapped.

j: the index of the other element to be swapped.

Example

Compile and Run

Output:

 [Georgia, Ginny, Daisy]

Explanation

It the above code, we have created a list of names: "Daisy", "Ginny" and "Georgia". The Collections.swap() method is used to swap the elements at position 0 and 2 which means "Daisy" and "Georgia" switch places.

After swapping, the list becomes [Georgia, Ginny, Daisy]. This method is useful when we want to change the order of specific elements quickly in a list without writing extra code.

Collections.copy() Method

The copy () method copies elements from one list to another. The destination list must be large enough to hold all elements from the source list.

Syntax:

Example

Compile and Run

Output:

[A, B, C]

Explanation

In the above code, we have two lists: one is the source list ("A", "B", "C") and the other is destination list ("X", "Y", "Z"). The collections.copy() method copies all elements from the source list into the destination list, replacing the existing values from each position. After the copy, the destination list becomes [A, B, C].

Collections.emptyList() Method

The emptyList() method creates a list that has no elements and cannot be changed. This means we cannot add, remove, or update anything in the list.

Syntax:

Example

Compile and Run

Output:

[]

Explanation

In this example, we used Collection.emptyList() to make an empty list. It is a read only list, so no one can add or remove anything from it. This is useful when we need to return an empty list from a method, and we want to make sure it stays empty and safe from changes.

Collections.singletonList() Method

The method returns an immutable list containing only the specified object. The returned list is serializable. It returns an immutable list containing only the specified object.

Syntax:

Where,

T: the class of the objects in the list

o: the sole object to be stored in the returned list.

Example

Compile and Run

Output:

[Hello]

Explanation

The above method in Java is used to create a list that contains only one item and that cannot be changed. This means that we cannot add, remove or replace any element in it. It is helpful when we need a fixed, single-value list for quick use. The above example shows the list contains only the word "Hello", and when printed, it shows [Hello]. This method is often used when we want to return a one-item list without worrying that someone might modify it later.

Conclusion

The Collections class in Java gives us many easy-to-use tools for working with collections like lists, sets, and maps. It has built-in methods to do common tasks such as sorting, reversing, shuffling and copying. These methods help us to write less code and make the program simpler and easier to understand. Working with collections class can save time.