Accessing stream in random order
Tiramisu Mokka
kofemann at gmail.com
Wed May 5 16:58:08 UTC 2021
Hi all,
The Stream API makes a great job to work with various collections.
However some simple
operations are quite hard to achieve, for example collect the result
into a random order list.
The best solution I came with is:
Arrays.asList("a", "b", "c", "d").stream()
.collect(collectingAndThen(toList(), l -> {
Collections.shuffle(l); return l;}));
If Collections#shuffle would return the shuffled list, then this can
be written even more elegant:
Arrays.asList("a", "b", "c", "d").stream()
.collect(collectingAndThen(toList(), Collections::shuffle));
Thus two questions:
1) why stream has a way to sort, but not to shuffle
2) why Collections#shuffle doesn't returns the suffld list
Are there objectives to address those cases?
Thanks in advance,
-kofemann
/** caffeinated mutations of the core personality */
More information about the core-libs-dev
mailing list