Skip to content
Tutorials Center
My learning

Topics

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.

Java
Collections.reverse(list);
Collections.shuffle(list, new Random(42));
int top = Collections.max(numbers);

Try it

Java Reverse, rotate and swap

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.

Java Seeded shuffle, max, min and frequency

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

Java
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

  • reverse returns nothing and changes the list. For a reversed view or copy without mutation, Java 21 List.reversed() is an alternative.
  • Shuffle quality: without a Random argument it uses an internal shared generator, so results differ between runs. For tests and simulations pass new Random(seed); for security-sensitive shuffles use SecureRandom.
  • Complexity: reverse, swap and rotate are O(n) or better (swap is O(1) on an ArrayList); shuffle is O(n); max, min, frequency are O(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: max with natural order throws NullPointerException if it meets null; supply Comparator.nullsFirst(...) to cope.
  • Arrays.asList lists can be reversed, shuffled and rotated (they allow set) but not resized.
  • Streams alternative: stream.max(comparator) returns an Optional, which is friendlier for possibly empty input.