Java Data Type How to - Get a random ordering of a list of unique Strings








Question

We would like to know how to get a random ordering of a list of unique Strings.

Answer

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//ww w  . j a  va2  s  .  c  om
public class Main {
  public static void main(String[] argv) throws Exception {
    List<Integer> list = new ArrayList<Integer>();

    for (int i = 1; i <= 12; i++)
      list.add(new Integer(i));

    Collections.shuffle(list);
    System.out.println(list);
  }
}

The code above generates the following result.