You have a couple options here. A list is a bit different than an array when it comes to shuffling.

As you can see below, an array is faster than a list, and a primitive array is faster than an object array.

Sample Durations

List<Integer> Shuffle: 43133ns Integer[] Shuffle: 31884ns int[] Shuffle: 25377ns

Below, are three different implementations of a shuffle. You should only use Collections.shuffle if you are dealing with a collection. There is no need to wrap your array into a collection just to sort it. The methods below are very simple to implement.

ShuffleUtil Class

import java.lang.reflect.Array; import java.util.*; public class ShuffleUtil<T> { private static final int[] EMPTY_INT_ARRAY = new int[0]; private static final int SHUFFLE_THRESHOLD = 5; private static Random rand;

Main Method

public static void main(String[] args) { List<Integer> list = null; Integer[] arr = null; int[] iarr = null; long start = 0; int cycles = 1000; int n = 1000; // Shuffle List<Integer> start = System.nanoTime(); list = range(n); for (int i = 0; i < cycles; i++) { ShuffleUtil.shuffle(list); } System.out.printf("%22s: %dns%n", "List<Integer> Shuffle", (System.nanoTime() - start) / cycles); // Shuffle Integer[] start = System.nanoTime(); arr = toArray(list); for (int i = 0; i < cycles; i++) { ShuffleUtil.shuffle(arr); } System.out.printf("%22s: %dns%n", "Integer[] Shuffle", (System.nanoTime() - start) / cycles); // Shuffle int[] start = System.nanoTime(); iarr = toPrimitive(arr); for (int i = 0; i < cycles; i++) { ShuffleUtil.shuffle(iarr); } System.out.printf("%22s: %dns%n", "int[] Shuffle", (System.nanoTime() - start) / cycles); }

Shuffling a Generic List

// ================================================================ // Shuffle List<T> (java.lang.Collections) // ================================================================ @SuppressWarnings("unchecked") public static <T> void shuffle(List<T> list) { if (rand == null) { rand = new Random(); } int size = list.size(); if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) { for (int i = size; i > 1; i--) { swap(list, i - 1, rand.nextInt(i)); } } else { Object arr[] = list.toArray(); for (int i = size; i > 1; i--) { swap(arr, i - 1, rand.nextInt(i)); } ListIterator<T> it = list.listIterator(); int i = 0; while (it.hasNext()) { it.next(); it.set((T) arr[i++]); } } } public static <T> void swap(List<T> list, int i, int j) { final List<T> l = list; l.set(i, l.set(j, l.get(i))); } public static <T> List<T> shuffled(List<T> list) { List<T> copy = copyList(list); shuffle(copy); return copy; }

Shuffling a Generic Array

// ================================================================ // Shuffle T[] // ================================================================ public static <T> void shuffle(T[] arr) { if (rand == null) { rand = new Random(); } for (int i = arr.length - 1; i > 0; i--) { swap(arr, i, rand.nextInt(i + 1)); } } public static <T> void swap(T[] arr, int i, int j) { T tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } public static <T> T[] shuffled(T[] arr) { T[] copy = Arrays.copyOf(arr, arr.length); shuffle(copy); return copy; }

Shuffling a Primitive Array

// ================================================================ // Shuffle int[] // ================================================================ public static <T> void shuffle(int[] arr) { if (rand == null) { rand = new Random(); } for (int i = arr.length - 1; i > 0; i--) { swap(arr, i, rand.nextInt(i + 1)); } } public static <T> void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } public static int[] shuffled(int[] arr) { int[] copy = Arrays.copyOf(arr, arr.length); shuffle(copy); return copy; }

Utility Methods

Simple utility methods to copy and convert arrays to lists and vice-versa.

// ================================================================ // Utility methods // ================================================================ protected static <T> List<T> copyList(List<T> list) { List<T> copy = new ArrayList<T>(list.size()); for (T item : list) { copy.add(item); } return copy; } protected static int[] toPrimitive(Integer[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_INT_ARRAY; } final int[] result = new int[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].intValue(); } return result; } protected static Integer[] toArray(List<Integer> list) { return toArray(list, Integer.class); } protected static <T> T[] toArray(List<T> list, Class<T> clazz) { @SuppressWarnings("unchecked") final T[] arr = list.toArray((T[]) Array.newInstance(clazz, list.size())); return arr; }

Range Class

Generates a range of values, similar to Python's range function.