shuffle()

Shuffle the input stream into a random order.

Implementation Notes

This implementation reads the entire stream before emitting any results, making it inappropriate for infinite streams. There are two versions of this Gatherer, one that uses the platform default RandomGenerator and one that allows the caller to specify a RandomGenerator. Most RandomGenerator implementations are not thread-safe, so a single instance of this Gatherer should not be used from multiple threads at the same time.

Signatures

  1. shuffle()
  2. shuffle(RandomGenerator randomGenerator)
  • randomGenerator - (Optional) A RandomGenerator to use as the source of randomness.

Examples

Randomly shuffle the input stream specifying the source of randomness

Stream
    .of("A", "B", "C", "D", "E", "F", "G")
    .gather(Gatherers4j.shuffle(new Random(42)))
    .toList();
    
// [ "B", "D", "F", "A", "E", "G", "C" ]

Randomly shuffle the input stream with the default source of random

Stream
    .of("A", "B", "C", "D", "E", "F", "G")
    .gather(Gatherers4j.shuffle())
    .toList();
    
// [ "F", "D", "A", "G", "B", "C", "E" ]  <-- Random!