Collections.sort() / Collections.shuffle() / Collections.reverse()
Utility methods for sorting a List, shuffling elements randomly, reversing order, and retrieving the minimum or maximum value. These are provided as static methods of the java.util.Collections class.
Syntax
import java.util.Collections; // Sorts the list in ascending order (destructive operation). Collections.sort(List<T> list); Collections.sort(List<T> list, Comparator<? super T> c); // Shuffles the list elements randomly (destructive operation). Collections.shuffle(List<?> list); // Reverses the order of elements in the list (destructive operation). Collections.reverse(List<?> list); // Returns the minimum or maximum value in the list. Collections.min(Collection<? extends T> coll); Collections.max(Collection<? extends T> coll);
Method List
| Method | Description |
|---|---|
| Collections.sort(list) | Sorts the list in-place in natural order (ascending). Elements must implement Comparable. |
| Collections.sort(list, comparator) | Sorts the list in a custom order using a Comparator. |
| Collections.shuffle(list) | Randomly shuffles the elements of the list. Useful for things like shuffling a deck of cards in a game. |
| Collections.reverse(list) | Reverses the order of elements in the list. |
| Collections.min(coll) | Returns the minimum element in the collection. Elements must implement Comparable. |
| Collections.max(coll) | Returns the maximum element in the collection. |
Sample Code
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
ArrayList<Integer> nums = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6));
// Use sort() to sort in ascending order.
Collections.sort(nums);
System.out.println(nums); // Prints "[1, 1, 2, 3, 4, 5, 6, 9]".
// Use a Comparator to sort in descending order.
Collections.sort(nums, Collections.reverseOrder());
System.out.println(nums); // Prints "[9, 6, 5, 4, 3, 2, 1, 1]".
// Use a lambda to sort by string length.
ArrayList<String> words = new ArrayList<>(Arrays.asList("banana", "fig", "apple"));
Collections.sort(words, (a, b) -> a.length() - b.length());
System.out.println(words); // Prints "[fig, apple, banana]".
// Use shuffle() to randomize order.
Collections.shuffle(nums);
System.out.println(nums); // Prints elements in a random order.
// Use reverse() to reverse the order.
ArrayList<String> letters = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
Collections.reverse(letters);
System.out.println(letters); // Prints "[d, c, b, a]".
// Use min() and max() to get the smallest and largest values.
ArrayList<Integer> scores = new ArrayList<>(Arrays.asList(85, 92, 78, 95, 61));
System.out.println(Collections.min(scores)); // Prints "61".
System.out.println(Collections.max(scores)); // Prints "95".
Notes
As of Java 8, Collections.sort() can also be called as an instance method on a List, using the form list.sort(comparator). Internally, it uses the TimSort algorithm, which is a stable sort — meaning the relative order of equal elements is preserved.
These methods modify the original list directly (destructive operations). If you need to preserve the original order, create a copy first using something like new ArrayList<>(original) before calling these methods.
To add or retrieve list elements, see add() / get(). To work with maps, see HashMap's put() / get().
If you find any errors or copyright issues, please contact us.