Shuffle Elements of ArrayList in Java - GeeksforGeeks

07 Jan, 2021
Shuffling means changing the positions of ArrayList elements randomly. After shuffling, they will be in different order. 
Following is the example of Shuffling ArrayList elements.
Ways to shuffle elements of ArrayList:
Using Random classUsing Collections.shuffle()
Using Random class
Using Collections.shuffle()
Method 1: Using Random class
In this method we will be going to shuffle ArrayList element using Random class to generate random index.
And java collections.swap() method to swap ArrayList elements.
And one more algorithm we will be going to use that is Fisher–Yates shuffle.
Code:
Java
// Java program to demonstrate shuffling of arraylist// elements Using Random class  import java.util.*;class ArraylistShuffle {    public static void main(String args[])    {        // creating        // ArrayList        ArrayList<Integer> al = new ArrayList<Integer>();          // adding object in ArrayList        al.add(10);        al.add(20);        al.add(30);        al.add(40);        al.add(50);        al.add(60);        al.add(70);        al.add(80);          System.out.println("Before shuffling Arraylist:");          // getting Iterator        // from arraylist to        // traverse elements        Iterator itr = al.iterator();          while (itr.hasNext()) {            System.out.print(itr.next() + " ");        }          System.out.println("");          Random r1 = new Random();          for (int i = al.size() - 1; i >= 1; i--) {            // swapping current index value            // with random index value            Collections.swap(al, i, r1.nextInt(i + 1));        }          System.out.println("After shuffling Arraylist:");          itr = al.iterator();          while (itr.hasNext()) {            System.out.print(itr.next() + " ");        }    }}
Before shuffling Arraylist:
10 20 30 40 50 60 70 80 
After shuffling Arraylist:
10 60 30 20 50 80 40 70 
Method 2: Using Collections.shuffle()
Shuffle ArrayList Elements by using Collections.shuffle().
Java
// Java program to demonstrate shuffling// ArrayList elements using Collections.shuffle()  import java.util.*;class ArraylistShuffle {    public static void main(String args[])    {        // creating ArrayList        ArrayList<String> al = new ArrayList<String>();          // adding object in ArrayList        al.add("C");        al.add("C++");        al.add("Java");        al.add("Python");        al.add("PHP");        al.add("Javascript");          System.out.println("Before shuffling Arraylist:");          // getting Iterator        // from arraylist to        // traverse elements        Iterator itr = al.iterator();          while (itr.hasNext()) {            System.out.print(itr.next() + " ");        }          System.out.println("");          Collections.shuffle(al);          System.out.println("After shuffling Arraylist:");          itr = al.iterator();          while (itr.hasNext()) {            System.out.print(itr.next() + " ");        }    }}
Before shuffling Arraylist:
C C++ Java Python PHP Javascript 
After shuffling Arraylist:
PHP Java C++ C Javascript Python 
Java-ArrayList
Java-Collections
Picked
Java
Java Programs
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character