
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 the Elements of a Collection
The Collection is a framework that provides an architecture to store and manipulate a group of objects. In Java, Collections can perform all the operations that you perform on data, such as searching, sorting, insertion, manipulation, and deletion.
In this article, we will learn how to shuffle the elements of a collection in Java. Shuffling refers to randomly rearranging the elements of a collection. Let's take an example as follows:
Input: list: [Java, program, is, fun, and, easy] Output: list: [fun, easy, Java, program, and, is]
Ways to Shuffle Elements of a Collection in Java
- Using Collections.shuffle() Method
- Using Java Streams
- Using Custom Algorithm
Using Collections.shuffle() Method
Java provides a built-in method Collections.shuffle()
It is mainly used for shuffling the elements of a collection. This method uses the Random instance to randomly permute the elements of the list.
Example
In the following example, we will create a list and then shuffle its elements using the Collections.shuffle()
method from the Collections class.
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class ShuffleCollection { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("program"); list.add("is"); list.add("fun"); list.add("and"); list.add("easy"); System.out.println("Original List: " + list); Collections.shuffle(list, new Random()); System.out.println("Shuffled List: " + list); } }
Following is the output of the above code:
Original List: [Java, program, is, fun, and, easy] Shuffled List: [fun, easy, Java, program, and, is]
Using Java Streams
We can also shuffle the elements of a collection using the Stream API. First, we convert the collection to a stream, collect it into a list, and then use the Collections.shuffle()
method to shuffle the elements.
Example
In the following example, we will create a list and then shuffle its elements using the Stream API.
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ShuffleCollectionUsingStreams { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("program"); list.add("is"); list.add("fun"); list.add("and"); list.add("easy"); System.out.println("Original List: " + list); List<String> shuffledList = Stream.of(list.toArray(new String[0])) .collect(Collectors.toList()); Collections.shuffle(shuffledList); System.out.println("Shuffled List: " + shuffledList); } }
Following is the output of the above code:
Original List: [Java, program, is, fun, and, easy] Shuffled List: [fun, easy, Java, program, and, is]
Using Fisher-Yates shuffle Algorithm
We can also implement our custom algorithm to shuffle the elements of a collection. One of the simplest algorithms is the Fisher-Yates shuffle algorithm, which iterates through the collection and swaps each element with a randomly chosen element that comes after it (including itself).
Steps to Implement the Fisher-Yates Shuffle Algorithm
- Start from the last element of the collection and swap it with a randomly chosen element from the entire collection.
- Move to the second last element and repeat the process until you reach the first element.
- Continue this process until all elements have been shuffled.
Example
In the following example, we will implement the Fisher-Yates shuffle algorithm to shuffle the elements of a list.
import java.util.ArrayList; import java.util.List; import java.util.Random; public class CustomShuffle { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("program"); list.add("is"); list.add("fun"); list.add("and"); list.add("easy"); System.out.println("Original List: " + list); shuffle(list); System.out.println("Shuffled List: " + list); } public static void shuffle(List<String> list) { Random random = new Random(); for (int i = list.size() - 1; i > 0; i--) { int j = random.nextInt(i + 1); String temp = list.get(i); list.set(i, list.get(j)); list.set(j, temp); } } }
Following is the output of the above code:
Original List: [Java, program, is, fun, and, easy] Shuffled List: [fun, easy, Java, program, and, is]