The Collections Utility Class in Java
Collections, from java.util (not to be confused with the Collection interface it operates on), is a class of static utility methods for working with lists and other collections as a whole: sort() orders a list in place, either by natural ordering or a supplied Comparator; reverse() flips a list's order; max() and min() find the largest or smallest element; and shuffle() randomizes element order.
These methods generally work directly on the collection passed in — sort() and reverse() both modify the original list in place rather than returning a new one. Several, like unmodifiableList(), wrap an existing collection to produce a read-only view that throws if anything tries to change it, useful for handing data out to code that shouldn't be able to modify the original.
sort()
Orders a list in place, using either the elements' natural ordering or an explicit Comparator passed as a second argument.
reverse()
Flips a list's element order in place.
max() and min()
Scan a collection to find its largest or smallest element, again respecting natural ordering or a supplied Comparator.
shuffle()
Randomizes a list's element order in place, useful for anything from card games to randomized testing.
unmodifiableList() and similar
Wrap an existing collection in a read-only view that throws an exception if any modification is attempted through it.
import java.util.ArrayList;import java.util.Collections; public class CollectionsUtilDemo { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(3); numbers.add(1); numbers.add(2); Collections.sort(numbers); System.out.println("Sorted: " + numbers); System.out.println("Max: " + Collections.max(numbers)); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.