Skip to content

Take, Skip and Sample

johnmcclean-aol edited this page Feb 16, 2015 · 1 revision

NB

SimpleReact flows are concurrent. Skipping / Taking / Sampling will operate based on the completion order of Stream elements, not the order of data input into the Stream.

Sampling

To select every second member of a Stream, filter with Predicates.sample(2) - and so on for any simple sample rate. To take 1% of a Stream use Predicates.sample(100)

List<String> titles = new SimpleReact().reactToCollection(collection)
							.<String>then(url -> getTitle(url))
							.filter(Objects::nonNull)
							.filter(Predicates.sample(2))
							.peek(title -> saveTitle(title) )
							.peek(System.out::println)
							.block();

Skip

To Skip the first five members of a Stream use Predicates.skip(5)

List<String> titles = new SimpleReact().reactToCollection(collection)
							.<String>then(url -> getTitle(url))
							.filter(Objects::nonNull)
							.filter(Predicates.skip(5))
							.peek(title -> saveTitle(title) )
							.peek(System.out::println)
							.block();

Take (or Limit)

To limit your resultset to 10 members use Predicates.take (or Predicates.limit)

List<String> titles = new SimpleReact().reactToCollection(collection)
							.<String>then(url -> getTitle(url))
							.filter(Objects::nonNull)
							.filter(Predicates.take(5))
							.peek(title -> saveTitle(title) )
							.peek(System.out::println)
							.block();
Clone this wiki locally