Java Collection Tutorial - Java Collections.shuffle(List <?> list)








Syntax

Collections.shuffle(List <?> list) has the following syntax.

public static void shuffle(List <?> list)

Example

In the following code shows how to use Collections.shuffle(List <?> list) method.

//from w  w  w  .j  av  a 2 s  .  c o m
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
   public static void main(String args[]) {
      // create array list object       
      List<String> arrlist = new ArrayList<String>();
      
      // populate the list
      arrlist.add("A");
      arrlist.add("B");
      arrlist.add("from java2s.com");  
      
      System.out.println("Initial collection: "+arrlist);
      
      // shuffle the list
      Collections.shuffle(arrlist);
      
      System.out.println("Final collection after shuffle: "+arrlist);
   }    
}

The code above generates the following result.