Shuffle elements of ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Shuffle elements of ArrayList

Demo Code


import java.util.ArrayList;
import java.util.Collections;
 
public class Main {
 
  public static void main(String[] args) {
   //  w w  w .  j a  v  a2 s. co  m
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
   
    //Add elements to Arraylist
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("E");
   
    System.out.println("Before shuffling, ArrayList contains : " + arrayList);
   
    Collections.shuffle(arrayList);
   
    System.out.println("After shuffling, ArrayList contains : " + arrayList);
   
  }
}
 

Result


Related Tutorials