I tried to translate the following line of Scala to Java 8 using the Streams API:
// Scala
util.Random.shuffle((1 to 24).toList)
To write t
You may find the following toShuffledList() method useful.
private static final Collector, ?, ?> SHUFFLER = Collectors.collectingAndThen(
Collectors.toCollection(ArrayList::new),
list -> {
Collections.shuffle(list);
return list;
}
);
@SuppressWarnings("unchecked")
public static Collector> toShuffledList() {
return (Collector>) SHUFFLER;
}
This enables the following kind of one-liner:
IntStream.rangeClosed('A', 'Z')
.mapToObj(a -> (char) a)
.collect(toShuffledList())
.forEach(System.out::print);
Example output:
AVBFYXIMUDENOTHCRJKWGQZSPL