Java Examples - Collection Shuffle

Advertisements


Problem Description:

How to shuffle the elements of a collection ?

Solution:

Following example how to shuffle the elements of a collection with the help of Collections.shuffle() method of Collections class.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
   public static void main(String[] argv) 
   throws Exception {
      String[] alpha = { "A", "E", "I", "O", "U" };
      List list = new ArrayList(alpha);
      Collections.shuffle(list);
      System.out.println("list");
   }   
}

Result:

The above code sample will produce the following result.

I
O
A
U
E


Advertisements
Advertisements