Java Collection How to - Shuffle a list








Question

We would like to know how to shuffle a list.

Answer

//  ww  w  .  j  av  a  2 s. c  om
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Shuffle {
  public static void main(String args[]) {
    String[] strArray = new String[] { "Java", "Source", "And", "and",
        "Support", "java2s" };

    List l = Arrays.asList(strArray);
    Collections.shuffle(l);
    System.out.println(l);
  }
}

The code above generates the following result.