Shuffle Elements of ArrayList in Java Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report 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 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() + " "); } } } Output 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() + " "); } } } Output Before shuffling Arraylist: C C++ Java Python PHP Javascript After shuffling Arraylist: PHP Java C++ C Javascript Python Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer: Comprehensive Course Expert Guidance for Efficient Learning Hands-on Experience with Real-world Projects Proven Track Record with 100,000+ Successful Geeks Last Updated : 07 Jan, 2021 Like Article Save Article Previous How to Add Element in Java ArrayList? Next Java Program to Get the Size of Collection and Verify that Collection is Empty Share your thoughts in the comments Add Your Comment Please Login to comment...