!7
p-exp {
pnl(iteratorToList(shuffledIterator(words("This is a very good test"))));
}
static Iterator shuffledIterator(final List l) {
return new Iterator() {
Random randomizer = new Random();
int n = l.size();
int i = 0;
Map shuffled = new HashMap();
public boolean hasNext() { ret i < n; }
public A next() {
int j = i+randomizer.nextInt(n-i);
A a = get(i), b = get(j);
shuffled.put(j, a);
shuffled.remove(i);
++i;
return b;
}
A get(int i) {
return shuffled.containsKey(i) ? shuffled.get(i) : l.get(i);
}
};
}