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








Syntax

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

public static void shuffle(List<?> list,  Random rnd) 

Example

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

/*  w  w w.j  ava2s .c  o m*/
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class Main {
   public static void main(String args[]) {  
      // create Linked List
      List<Integer>  list = new LinkedList<Integer> (); 
      
      // populate list
      list.add(5);  
      list.add(2);  
      list.add(1);  
      list.add(-3);
      
      System.out.println("List before shuffle: "+list);   

      // shuffle the list
      Collections.shuffle(list,new Random());  
    
      System.out.println("List after shuffle: "+list);
   }
}

The code above generates the following result.