Collections.shuffle() Method in Java with Examples
The shuffle() method is part of the java.util package that shuffles or Rearranges the elements present in the list. The shuffle() destroys any kind of order present in the data structure. It just does the opposite of sorting. The shuffle() method operates in linear time.
The implementation of the shuffle() method iterates in the list backwards, starting from the last element up to the second element, continuously performing a swap on a randomly selected element into the "current position". The Elements are randomly chosen from the part of the list that runs from the first element to the current position, inclusive.
There exist 2 different types of the shuffle() method using which the elements from the list can be permuted. The types are:
1. Collections.shuffle(list) method
This type shuffles the list using a pre-defined source of randomness. All the permutations that occur are with almost equal likelihood
Syntax:
public static void shuffle(List<?> list)
Parameter:
This type takes in one parameter.
- List
2. Collections.shuffle(list, random) method
This type shuffles the list using a user-defined source of randomness.
Syntax:
public static void shuffle(List<?> list, Random random)
Parameter:
This type takes in two parameters.
- List
- The source of the randomness.
Exception
The shuffle() method throws the UnsupportedOperationException if the mentioned list or its list-iterator does not support the set operation.
Returns
The shuffle() method does not return anything.
Compatibility Required
It requires Java 1.5 or above.
Now, go through some examples of the shuffle() method given below.
Example 1
In the example below, we have created a list and shuffled its elements using the shuffle(list) method, which uses a predefined source of randomness.
FileName: ShuffleExample.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ShuffleExample {
// Main method
public static void main(String[] args) {
List<String> list = new ArrayList<>();
//Add elements to the list
list.add("Java");
list.add("Spring");
list.add("Spring Boot");
list.add("Hibernate");
list.add("Spring Security");
// Display the elements before shuffling
System.out.println("List before shuffling: "+list);
// shuffles the list
Collections.shuffle(list);
// Display the elements after shuffling
System.out.println("List after shuffling: "+list);
}
}
Output:
List before shuffling: [Java, Spring, Spring Boot, Hibernate, Spring Security]
List after shuffling: [Java, Hibernate, Spring Boot, Spring Security, Spring]
Example 2
In the example below, we have created a linkedList and shuffled its elements using the shuffle(list) method that uses a predefined source of randomness.
FileName: ShuffleExample1.java
import java.util.Collections;
import java.util.LinkedList;
public class ShuffleExample1 {
// Main method
public static void main(String[] args) {
// Creates a LinkedList
LinkedList<Integer> linkedList = new LinkedList<>();
//Add elements in the linkedList
linkedList.add(100);
linkedList.add(200);
linkedList.add(300);
linkedList.add(400);
linkedList.add(500);
// Display the elements before shuffling
System.out.println("List before shuffling: "+linkedList);
// shuffles the list
Collections.shuffle(linkedList);
// Display the elements after shuffling
System.out.println("List after shuffling: "+linkedList);
}
}
Output:
List before shuffling: [100, 200, 300, 400, 500]
List after shuffling: [500, 100, 400, 300, 200]
Example 3
In the example below, we created a linked list and shuffled its elements using the shuffle(list) and the shuffle(list, random) method.
FileName: ShuffleExample2.java
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
public class ShuffleExample2 {
// Main method
public static void main(String[] args) {
// Creates a LinkedList
LinkedList<String> sports = new LinkedList<>();
//Add elements in the linkedList
sports.add("Cricket");
sports.add("Soccer");
sports.add("Hockey");
sports.add("Tennis");
sports.add("BasketBall");
sports.add("Golf");
// Display the elements before shuffling
System.out.println("List before shuffling: "+sports);
// shuffles the list
Collections.shuffle(sports, new Random());
// Display the elements after shuffling
System.out.println("List after shuffling: "+sports);
// shuffles the list
Collections.shuffle(sports, new Random(2));
// Display the elements after shuffling
System.out.println("List after shuffling: "+sports);
}
}
Output:
List before shuffling: [Cricket, Soccer, Hockey, Tennis, BasketBall, Golf]
List after shuffling: [Tennis, Hockey, Golf, Soccer, Cricket, BasketBall]
List after shuffling: [BasketBall, Tennis, Hockey, Soccer, Golf, Cricket]
Example 4
The program below shuffles the elements from the list using the shuffle(list, random) that uses a user-defined source of randomness.
FileName: ShuffleExample3.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class ShuffleExample3 {
// Main method
public static void main(String[] args) {
// Creates an ArrayList
ArrayList<Integer> arrayList = new ArrayList<>();
//Add elements in the arrayList
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
arrayList.add(5);
arrayList.add(6);
// Display the elements before shuffling
System.out.println("List before shuffling: "+arrayList);
// shuffles the list
Collections.shuffle(arrayList, new Random(3));
// Display the elements after shuffling
System.out.println("List after shuffling: "+arrayList);
}
}
Output:
List before shuffling: [1, 2, 3, 4, 5, 6]
List after shuffling: [6, 4, 2, 5, 1, 3]