
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
Shuffle Elements of a Collection in Java
In this article, we will understand how to shuffle the elements of a collection. The Collection is a framework that provides an architecture to store and manipulate the group of objects.
In Java, Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
Below is a demonstration to Shuffle the Elements of a Collection ?
Suppose our input is ?
Input list: [Java, program, is, fun, and, easy]
The desired output would be ?
The shuffled list is: [is, easy, program, and, fun, Java]
Algorithm
Here, we have a clear and understandable algorithm to shuffle the elements of a collection ?
Step 1 - START Step 2 - Declare an arraylist namely input_list. Step 3 - Define the values. Step 4 - Using the function shuffle(), we shuffle the elements of the list. Step 5 - Display the result Step 6 - Stop
To Shuffle the Elements of a Collection in Java is quite easy. Let us learn the following approaches:
Simple List Shuffling using the Main Method
This approach is used to bind all list operations within the main method. It demonstrates a straightforward way to shuffle an ArrayList using Collections.shuffle() with a Random instance.
Example
In this program, we initialize an 'ArrayList' of strings, add elements to it, and print the list. It then shuffles the list using 'Collections.shuffle()' with a 'Random' instance.
import java.util.*; public class Demo { public static void main(String[] args){ ArrayList<String> input_list = new ArrayList<String>(); input_list.add("Java"); input_list.add("program"); input_list.add("is"); input_list.add("fun"); input_list.add("and"); input_list.add("easy"); System.out.println("The list is defined as:" + input_list); Collections.shuffle(input_list, new Random()); System.out.println("The shuffled list is: \n" + input_list); } }
Output
The above program produce the following result ?
The list is defined as:[Java, program, is, fun, and, easy] The shuffled list is: [is, Java, fun, program, easy, and]
Encapsulated List Shuffling using Object-Oriented Style
This is another approach to encapsulate the shuffling operations within a separate method, demonstrating an object-oriented style. This organizes the code into distinct functions for better clarity and modularity.
Example
The program defines a 'shuffle' method to shuffle an 'ArrayList' of strings using 'Collections.shuffle()'. The main method initializes the list and calls the shuffle method to shuffle the list.
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
import java.util.*; public class Demo { static void shuffle(ArrayList<String> input_list){ Collections.shuffle(input_list, new Random()); System.out.println("The shuffled list is: \n" + input_list); } public static void main(String[] args){ ArrayList<String> input_list = new ArrayList<String>(); input_list.add("Java"); input_list.add("program"); input_list.add("is"); input_list.add("fun"); input_list.add("and"); input_list.add("easy"); System.out.println("The list is defined as:" + input_list); shuffle(input_list); } }
Output
The above program produce the following result ?
The list is defined as:[Java, program, is, fun, and, easy] The shuffled list is: [fun, and, Java, easy, is, program]