In Java, Collections.shuffle(list) puts the elements of a List into a random order. Every possible order is equally likely, assuming a fair source of randomness. The list is changed in place; the method returns nothing.
The second overload, shuffle(list, rnd), uses the java.util.Random object you pass in. Two Random objects created with the same seed produce the same shuffle, which is handy for tests and replayable games.
A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.shuffle(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Pass the list, and optionally a Random.
Java
List<Integer> deck = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Collections.shuffle(deck); // some random order
Collections.shuffle(deck, new Random(7)); // repeatable for seed 7
Call
What it does
Collections.shuffle(list)
Random order using a shared default source of randomness.
Collections.shuffle(list, rnd)
Random order driven by your Random object.
Collections.shuffle(list, new Random(seed))
Same seed, same result every run.
Collections.sort(list)
Puts a shuffled list back into order.
The algorithm walks from the last index down to 1 and swaps each element with a randomly chosen one at or before it. Because the result is random, the examples below never print a particular order; they check properties that are always true. Java 21 also added an overload that accepts a java.util.random.RandomGenerator.
Library declarations
Java
// java.util.Collections
public static void shuffle(List<?> list)
public static void shuffle(List<?> list, Random rnd)
// Java 21+: public static void shuffle(List<?> list, RandomGenerator rnd)
Parameters
Parameter
Description
list
The list to shuffle. It must support set.
rnd
The source of randomness to use (optional).
Return Value
Return Value
Description
void
Nothing. The same elements are now in a random order.
Example 1 — Shuffle Cards and Check Nothing Was Lost
Shuffle ten cards, then prove the deck still holds exactly the same cards.
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsShuffleDeck {
public static void main(String[] args) {
List<Integer> deck = new ArrayList<>();
for (int card = 1; card <= 10; card++) {
deck.add(card);
}
List<Integer> original = new ArrayList<>(deck);
Collections.shuffle(deck);
System.out.println("Size still " + deck.size());
System.out.println("Same cards: " + deck.containsAll(original) + " " + original.containsAll(deck));
List<Integer> sortedCopy = new ArrayList<>(deck);
Collections.sort(sortedCopy);
System.out.println("Sorting restores 1..10: " + sortedCopy.equals(original));
}
}
Output
Size still 10
Same cards: true true
Sorting restores 1..10: true
How It Works
Keep a reference copy.original saves the cards 1 to 10 before the shuffle.
Random, but complete.shuffle only swaps positions, so no card is added or lost and the size stays 10.
Two-way containsAll. Each list contains every element of the other, which confirms the same set of cards.
Order can be recovered. Sorting a copy of the shuffled deck gives back 1 to 10 exactly, without printing the random order itself.
Example 2 — Same Seed, Same Shuffle
Two lists shuffled with Random objects that share a seed end up in the same order.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsShuffleSeed {
public static void main(String[] args) {
List<String> a = new ArrayList<>(Arrays.asList("red", "green", "blue", "gold", "pink"));
List<String> b = new ArrayList<>(a);
Collections.shuffle(a, new Random(42));
Collections.shuffle(b, new Random(42));
System.out.println("Same seed, same order: " + a.equals(b));
System.out.println("Still 5 colors: " + (a.size() == 5));
System.out.println("gold still there: " + a.contains("gold"));
}
}
Output
Same seed, same order: true
Still 5 colors: true
gold still there: true
How It Works
Identical starting lists.b is a copy of a, so both begin as red, green, blue, gold, pink.
Matching random sources. Each list gets its own new Random(42); equal seeds produce the same sequence of random numbers.
Equal results. The same numbers drive the same swaps, so a.equals(b) is true whatever the actual order is.
Contents unchanged. The size and membership checks confirm that shuffling moved colors without changing them.
Example 3 — Tiny Lists and Read-Only Lists
Shuffling one element or none is safe. Shuffling a read-only list with several elements is not.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsShuffleEdges {
public static void main(String[] args) {
List<String> one = new ArrayList<>(Arrays.asList("only"));
Collections.shuffle(one);
System.out.println("One element: " + one);
List<Integer> none = new ArrayList<>();
Collections.shuffle(none);
System.out.println("Empty: " + none);
List<Integer> locked = Collections.unmodifiableList(Arrays.asList(1, 2, 3));
try {
Collections.shuffle(locked);
} catch (UnsupportedOperationException e) {
System.out.println("Read-only: " + e.getClass().getSimpleName());
}
}
}
Output
One element: [only]
Empty: []
Read-only: UnsupportedOperationException
How It Works
Nothing to mix. A list with one element has only one possible order, so it prints [only] every time.
Empty is fine. Shuffling an empty list does no swaps and throws nothing.
Swaps need set. With three elements, shuffle must write elements back, and the unmodifiable wrapper rejects that with UnsupportedOperationException.
Frequently Asked Questions
It puts the elements of a List into a random order, in place. All possible orders are equally likely when the source of randomness is fair.
Pass a Random with a fixed seed: Collections.shuffle(list, new Random(42)). The same seed gives the same order on the same JDK.
No. It returns void and changes the list you pass in. Copy the list first if you need the original order.
Check properties instead of one exact order: the size is unchanged, the same elements are present, and sorting a copy restores the original.
For an object array, shuffle its list view: Collections.shuffle(Arrays.asList(array)) shuffles the array itself. For an int[] you need a manual loop or a boxed Integer[].