|
Simple Shuffle
The following code shows shuffling a list using Collections API.
|
package com.bethecoder.tutorials.random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SimpleShuffle {
public static void main(String[] args) {
List list = new ArrayList();
list.addAll(Arrays.asList(new String [] {
"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX"
}));
shuffle(list);
List list2 = new ArrayList();
list2.addAll(Arrays.asList(new Integer [] {
24, 345, 45, 67, 90, 93, 245, 678, 23
}));
shuffle(list2);
}
public static void shuffle(List list) {
System.out.println("Before : " + list);
Collections.shuffle(list);
System.out.println("after : " + list);
}
}
|
| |
It gives the following output,
Before : [ONE, TWO, THREE, FOUR, FIVE, SIX]
after : [SIX, TWO, THREE, ONE, FOUR, FIVE]
Before : [24, 345, 45, 67, 90, 93, 245, 678, 23]
after : [93, 67, 345, 678, 245, 90, 24, 23, 45]
|
|