Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to shuffle an array using list
In this article, we will learn how to shuffle an array using a list in Java. To do so, we will be using:
Using Collections.shuffle() Method
Shuffling an array means randomly rearranging its elements. The Java Collections framework provides the Collections.shuffle() Method, which shuffles a list. Since this method shuffles the list randomly, the order of the resultant elements is different each time we use it.
Example
Following is the program to shuffle an array using a list:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] args) {
String str[] = {"A", "B", "C", "D", "E"};
List<String>list = Arrays.asList(str);
Collections.shuffle(list);
System.out.println("Shuffled the array using List = "+list.toString());
}
}
Following is the output of the above program:
Shuffled the array using List = [C, B, E, D, A]
Using Fisher-Yates Shuffle
We can also implement our custom algorithm to shuffle the elements of a list. One simple and effective algorithm is the Fisher-Yates shuffle.
In this algorithm, we loop through the list from the last element to the first, and for each element, we swap it with a randomly selected element from the range 0 to the current index (inclusive). This gives us a well-randomized list.
Example
Following is an example to shuffle an array using a list by implementing the Fisher-Yates shuffle:
import java.util.*;
public class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
Random rand = new Random();
for (int i = list.size() - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
String temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
System.out.println("Shuffled list: " + list);
}
}
Following is the output of the above program:
Shuffled list: [D, B, A, C, E]
Using Java Streams
We can also shuffle the elements of a list using the Stream API. First, we convert the list into a stream, collect the elements into a new list. Invoke the Collections.shuffle() method on the newly obtained list.
This approach gives us a shuffled copy of the original list without changing it.
Example
In the following example, we will create a list and then shuffle its elements using the Stream API:
import java.util.*;
import java.util.stream.Collectors;
public class Demo {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C", "D", "E");
List<String> shuffled = list.stream()
.collect(Collectors.collectingAndThen(
Collectors.toList(), collected -> {
Collections.shuffle(collected);
return collected;
}
));
System.out.println("Shuffled list using streams: " + shuffled);
}
}
Following is the output of the above program:
Shuffled list using streams: [C, E, D, B, A]