Thursday, July 4, 2024
HomeLanguagesJavaShuffle Elements of ArrayList in Java

Shuffle Elements of ArrayList in Java

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:

  1. Using Random class
  2. 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 

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments