
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Shuffle the Elements of a Collection
In this article, we will understand how to shuffle the elements of a collection. The Collection is a framework that provides architecture to store and manipulate the group of objects. 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 of the same −
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
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
Example 1
Here, we bind all the operations together under the ‘main’ function.
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 list is defined as:[Java, program, is, fun, and, easy] The shuffled list is: [is, Java, fun, program, easy, and]
Example 2
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 list is defined as:[Java, program, is, fun, and, easy] The shuffled list is: [fun, and, Java, easy, is, program]
- Related Articles
- Java Program to Compare Elements in a Collection
- Golang program to shuffle the elements of an array
- Shuffle elements of ArrayList with Java Collections
- Java Program to append all elements of other Collection to ArrayList
- Write a Python program to shuffle all the elements in a given series
- Rotate elements of a collection in Java
- Java Program to insert all elements of other Collection to specified Index of ArrayList
- Java Program to shuffle an array using list
- Java Program to Print a Collection
- Java Program to Reverse a Collection
- Java Program to Get the Size of the Collection
- Append all elements of another Collection to a Vector in Java
- Program to find expected number of shuffle required to sort the elements of an array in Python
- C# Program to return a collection with repeated elements
- Java Program to Use Different Types of a Collection

Advertisements