- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 or Randomize a list in Java
To shuffle a list in Java, the code is as follows −
Example
import java.util.*; public class Demo{ public static void main(String[] args){ ArrayList<String> my_list = new ArrayList<String>(); my_list.add("Hello"); my_list.add(","); my_list.add("this"); my_list.add("is"); my_list.add("a"); my_list.add("sample"); System.out.println("The original list is : \n" + my_list); Collections.shuffle(my_list); System.out.println("\n The shuffled list is : \n" + my_list); } }
Output
The original list is : [Hello, ,, this, is, a, sample] The shuffled list is : [a, is, ,, Hello, this, sample]
A class named Demo contains the main function. Here, an array list is defined and elements are added to the array list with the help of the ‘add’ function. The original list is printed, and then the ‘shuffle’ function is called on this array list. This way, the elements in the list will be shuffled and then printed on the screen.
- Related Articles
- How to randomize and shuffle array of numbers in Java?
- How to randomize (shuffle) a JavaScript array?
- How to shuffle a List in Java
- Java Program to shuffle an array using list
- How to randomize the items of a list in Python?
- How to shuffle a list of objects in Python?
- how to shuffle a 2D array in java correctly?
- How to shuffle an array in Java?
- Java Program to Shuffle the Elements of a Collection
- How to shuffle columns or rows of matrix in PyTorch?
- Randomize string in C#
- Shuffle elements of ArrayList with Java Collections
- In MySQL, how can we randomize set of rows or values in the result set?
- Randomize color by number in JavaScript
- How to randomize rows of a matrix in R?

Advertisements