Shuffle an ArrayList in Java
This post will discuss how to shuffle an ArrayList
in Java using Fisher–Yates shuffle algorithm and shuffle()
method provided by the Collections
class.
1. Using Fisher–Yates Shuffle Algorithm
The Fisher-Yates shuffle is a classic algorithm for generating random permutations of a finite sequence. It works by iterating over the elements of the list from the last to the first and swapping each element with a randomly chosen element from the remaining ones. It is an in-place algorithm that takes linear time and does not require any extra space. Following is a Java implementation of the Fisher–Yates shuffle algorithm, which shuffles a list from the last index to the first using a loop and the Collections.swap()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.*; class Main { // Generic method to randomize a list in Java using Fisher–Yates shuffle public static<T> void shuffle(List<T> list) { Random random = new Random(); // start from the end of the list for (int i = list.size() - 1; i >= 1; i--) { // get a random index `j` such that `0 <= j <= i` int j = random.nextInt(i + 1); // swap element at i'th position in the list with the element at // randomly generated index `j` Collections.swap(list, i, j); } } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); shuffle(list); System.out.println(list); } } |
Following is an equivalent version of the above algorithm, which shuffles a list starting from the first index to the last index:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; class Main { // Generic method to randomize a list in Java using Fisher–Yates shuffle public static<T> void shuffle(List<T> list) { Random random = new Random(); // start from the beginning of the list for (int i = 0; i < list.size() - 1; i++) { // get a random index `j` such that `i <= j <= n` int j = i + random.nextInt(list.size() - i); // swap element at i'th position in the list with the element at // randomly generated index `j` Collections.swap(list, i, j); } } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); shuffle(list); System.out.println(list); } } |
2. Using Collections.shuffle()
method
The shuffle()
is a built-in method from the java.util.Collections
class that randomly reorders the elements of a list. It is a simple and convenient way to shuffle a list, which uses the Fisher–Yates shuffle algorithm. It internally works by creating an intermediary array from the list, shuffling the array and converting the array back into a list. Here is an example of how to use this method:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Collections.shuffle(list); System.out.println(list); } } |
There is an overloaded version of the Collections.shuffle()
method that allows us to pass a java.util.Random
object as a second argument. This way, we can control the randomness of the shuffling process by using a fixed seed or a different random number generator. For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.security.SecureRandom; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // shuffle using a fixed seed Collections.shuffle(list, new Random(100)); System.out.println("After shuffling with Random(100): " + list); // shuffle using a secure random number generator Collections.shuffle(list, new SecureRandom()); System.out.println("After shuffling with SecureRandom(): " + list); } } |
That’s all about shuffling an ArrayList
in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)