- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 elements of ArrayList with Java Collections
In order to shuffle elements of ArrayList with Java Collections, we use the Collections.shuffle() method. The java.util.Collections.shuffle() method randomly permutes the list using a default source of randomness.
Declaration −The java.util.Collections.shuffle() method is declared as follows −
public static void shuffle(List <?> list)
Let us see a program to shuffle elements of ArrayList with Java Collections −
Example
import java.util.*; public class Example { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(7); list.add(8); list.add(3); list.add(9); System.out.println("Original list : " + list); Collections.shuffle(list); // shuffling the list System.out.println("Shuffled list : " + list); } }
Output
Original list : [1, 2, 7, 8, 3, 9] Shuffled list : [3, 8, 7, 9, 1, 2]
- Related Articles
- Swap elements of ArrayList with Java collections
- Replace All Elements Of ArrayList with with Java Collections
- Copy Elements of One ArrayList to Another ArrayList with Java Collections Class
- Reverse order of all elements of ArrayList with Java Collections
- Find maximum element of ArrayList with Java Collections
- Find minimum element of ArrayList with Java Collections
- Get Enumeration over ArrayList with Java Collections
- Perform Binary Search on ArrayList with Java Collections
- Replace all occurrences of specified element of ArrayList with Java Collections
- Sort ArrayList in Descending order using Comparator with Java Collections
- Java Program to Shuffle the Elements of a Collection
- Java Lambda Expression with Collections
- How to copy elements of ArrayList to Java Vector
- How to remove all elements of ArrayList in Java?
- Sort Elements in an ArrayList in Java

Advertisements