Java Collection How to - Randomly Generate From A Given Array








Question

We would like to know how to randomly Generate From A Given Array.

Answer

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/*from w w w .j  a v a  2 s .  com*/
public class Main {
    public static void main(String[] args) {
        String[] peoples = {"A","B","C","D","E","F"};
        List<String> names = Arrays.asList(peoples);
        Collections.shuffle(names);
        for (String name : names) {
            System.out.print(name + " ");
        }
    }
}

The code above generates the following result.