Java Reference ยท Collections
Java Collections.reverse(), shuffle() and max()
Reference for the handy Collections utilities reverse, shuffle, swap, rotate, max, min, frequency and nCopies, with seeded and deterministic examples.
Besides sorting, java.util.Collections offers small methods that rearrange a list in place (reverse, shuffle, swap, rotate) and ones that compute a value from any collection (max, min, frequency, disjoint). They remove a lot of hand-written loops.
The in-place methods need a modifiable List. The query methods accept any Collection.
Collections.reverse(list); Collections.shuffle(list, new Random(42)); int top = Collections.max(numbers);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public static void reverse(List<?> list) public static void shuffle(List<?> list) public static void shuffle(List<?> list, Random rnd) public static void swap(List<?> list, int i, int j) public static void rotate(List<?> list, int distance) public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) public static int frequency(Collection<?> c, Object o)
| Name | Description |
|---|---|
list |
The list to rearrange in place. |
rnd |
The random source; pass a seeded Random for reproducible shuffles. |
i, j |
Indexes to swap. |
distance |
Positions to rotate; positive moves elements toward the end, wrapping around. |
comp |
Comparator used by max and min; null means natural order. |
Returns: max and min return the element; frequency returns an int; the others return void.
Throws: UnsupportedOperationException for an unmodifiable list; IndexOutOfBoundsException from swap; NoSuchElementException from max and min on an empty collection.
Usage notes
reversereturns nothing and changes the list. For a reversed view or copy without mutation, Java 21List.reversed()is an alternative.- Shuffle quality: without a
Randomargument it uses an internal shared generator, so results differ between runs. For tests and simulations passnew Random(seed); for security-sensitive shuffles useSecureRandom. - Complexity:
reverse,swapandrotateareO(n)or better (swapisO(1)on anArrayList);shuffleisO(n);max,min,frequencyareO(n). - Ties in
max/min: if several elements are equally large, which one is returned is not specified, so do not rely on it. - Null elements:
maxwith natural order throwsNullPointerExceptionif it meetsnull; supplyComparator.nullsFirst(...)to cope. Arrays.asListlists can be reversed, shuffled and rotated (they allowset) but not resized.- Streams alternative:
stream.max(comparator)returns anOptional, which is friendlier for possibly empty input.