Java Collections shuffle() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

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
CallWhat 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

ParameterDescription
listThe list to shuffle. It must support set.
rndThe source of randomness to use (optional).

Return Value

Return ValueDescription
voidNothing. The same elements are now in a random order.

Compare with Similar Methods

Methods that reorder a list:

MethodHow it differs
sort(list)Puts the elements into a fixed, predictable order.
rotate(list, d)Shifts by a fixed distance; nothing random.
swap(list, i, j)Exchanges two chosen positions; shuffle does many random swaps.
reverse(list)Mirrors the order the same way every time.

Java Examples

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));
    }
}

How It Works

  1. Keep a reference copy. original saves the cards 1 to 10 before the shuffle.
  2. Random, but complete. shuffle only swaps positions, so no card is added or lost and the size stays 10.
  3. Two-way containsAll. Each list contains every element of the other, which confirms the same set of cards.
  4. 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"));
    }
}

How It Works

  1. Identical starting lists. b is a copy of a, so both begin as red, green, blue, gold, pink.
  2. Matching random sources. Each list gets its own new Random(42); equal seeds produce the same sequence of random numbers.
  3. Equal results. The same numbers drive the same swaps, so a.equals(b) is true whatever the actual order is.
  4. 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());
        }
    }
}

How It Works

  1. Nothing to mix. A list with one element has only one possible order, so it prints [only] every time.
  2. Empty is fine. Shuffling an empty list does no swaps and throws nothing.
  3. 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[].

Next: singleton()

Create an immutable Set holding exactly one element.

singleton() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful