Sep 30, 2009

[Java] Shuffle the contents of a Collection

I was in a situation where i need to shuffle the objects stored inside a collection. Agenda is to generate random questions. Every question is represented as a POJO & stored in a List. Now this does it all.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class Shuffler {
public static void main(String[] args) {
List argList = Arrays.asList(args);
Collections.shuffle(argList);
for (String arg : argList)
{
System.out.format("%s ", arg);
}
System.out.println();
}
}

No comments:

Post a Comment

You may also like these writeups

Related Posts with Thumbnails

Join our Gang