Reactive programming with Java 8 and simple-react : stream operations

John McClean
2 min readMar 25, 2015

--

JDK 8 Stream operations from java.util.stream.Stream are available on both EagerFutureStream and LazyFutureStream.

See Oracle Java Tutorials for more details : https://docs.oracle.com/javase/tutorial/collections/streams/index.html

This adds the following operators to simple-react LazyFutureStream and EagerFutureStream.

  • filter(Predicate<? super T>)
assertThat(of(1,1,1,2).filter(it -> it==1)
.collect(Collectors.toList())
.size(),
is(3));
  • map(Function<? super T, ? extends R>)
assertThat(of(1).map(it->it+100)
.collect(Collectors.toList())
.get(0),
is(101));
  • mapToInt(ToIntFunction<? super T>)
assertThat(of(“1”,”2",”3",”4")
.mapToInt(it -> Integer.valueOf(it))
.max()
.getAsInt(),
is(4));
  • mapToLong(ToLongFunction<? super T>)
assertThat(of(“1”,”2",”3",”4")
.mapToLong(it -> Long.valueOf(it))
.max()
.getAsLong(),
is(4l));
  • mapToDouble(ToDoubleFunction<? super T>)
assertThat(of(“1”,”2",”3",”4")
.mapToDouble(it -> Double.valueOf(it))
.max()
.getAsDouble(),
is(4d));
  • flatMap(Function<? super T, ? extends Stream<? extends R>>)
assertThat(
LazyFutureStream.of( asList(“1”,”10"),
asList(“2”),
asList(“3”),
asList(“4”))
.flatMap( list -> list.stream())
.collect(Collectors.toList())
,hasItem(“10”));
  • flatMapToInt(Function<? super T, ? extends IntStream>)
assertThat(of( asList(“1”,”10"), 
asList(“2”),
asList(“3”),
asList(“4”))
.flatMapToInt(list ->list.stream()
.mapToInt(Integer::valueOf))
.max()
.getAsInt(),
is(10));
  • flatMapToLong(Function<? super T, ? extends LongStream>)
assertThat(of( asList(“1”,”10"), 
asList(“2”),
asList(“3”),
asList(“4”))
.flatMapToLong(list -> list.stream()
.mapToLong(Long::valueOf))
.max()
.getAsLong(),
is(10l));
  • flatMapToDouble(Function<? super T, ? extends DoubleStream>)
assertThat(of( asList(“1”,”10"),  
asList(“2”),
asList(“3”),
asList(“4”))
.flatMapToDouble(list ->list.stream()
.mapToDouble(Double::valueOf))
.max()
.getAsDouble(),
is(10d));
  • distinct()
assertThat(of(1,1,1,2,1).distinct()
.collect(Collectors.toList())
.size(),
is(2));
  • sorted()
assertThat(of(1,5,3,4,2).sorted()
.collect(Collectors.toList()),
is(Arrays.asList(1,2,3,4,5)));
  • sorted(Comparator<? super T>)
assertThat(of(1,5,3,4,2)
.sorted((t1,t2) -> t2-t1)
.collect(Collectors.toList()),
is(Arrays.asList(5,4,3,2,1)));
  • peek(Consumer<? super T>)
of(1).map(it->it+100)
.peek(System.out::println)
.collect(Collectors.toList());
  • limit(long)
assertThat(of(1,2,3,4,5).limit(2)
.collect(Collectors.toList())
.size(),
is(2));
  • skip(long)
assertThat(of(1,2,3,4,5)
.skip(2)
.collect(Collectors.toList())
.size(),
is(3));
  • forEach(Consumer<? super T>)
List<Integer> list = new ArrayList<>();
of(1,5,3,4,2)
.forEach(it-> list.add(it));
assertThat(list,hasItem(1));
assertThat(list,hasItem(2));
assertThat(list,hasItem(3));
assertThat(list,hasItem(4));
assertThat(list,hasItem(5));
  • forEachOrdered(Consumer<? super T>)
List<Integer> list = new ArrayList<>();

of(1,5,3,4,2).forEachOrdered(it-> list.add(it));
  • toArray()
assertThat( Arrays.asList(1,2,3,4,5),
hasItem(of(1,5,3,4,2).toArray()[0]));
  • toArray(IntFunction)
assertThat( Arrays.asList(1,2,3,4,5),
hasItem(of(1,5,3,4,2).toArray(it->new Integer[it])[0]));
  • reduce(T, BinaryOperator)

Performs a reduction on the elements of this stream.

assertThat(of(1,2,3,4,5)
.map(it -> it*100)
.reduce( 50,(acc,next) -> acc+next),
is(1550));
  • reduce(BinaryOperator)

Performs a reduction on the elements of this stream.

assertThat(of(1,2,3,4,5).map(it -> it*100)
.reduce( (acc,next) -> acc+next)
.get(),
is(1500));
  • reduce(U, BiFunction, BinaryOperator)
assertThat(of(1,2,3,4,5)
.map(it -> it*100)
.reduce( 0, (acc, next) -> acc+next,
Integer::sum),
is(1500));
  • collect(Supplier, BiConsumer, BiConsumer)
List<Integer> list = of(1,2,3,4,5)
.collect(ArrayList::new,
ArrayList::add, ArrayList::addAll);

assertThat(list.size(),is(5));
  • collect(Collector<? super T, A, R>)
assertThat(of(1,2,3,4,5)
.collect(Collectors.toList())
.size(),is(5));
assertThat(of(1,1,1,2)
.collect(Collectors.toSet())
.size(),is(2));
  • min(Comparator<? super T>)
assertThat(of(1,2,3,4,5).min((t1,t2) -> t1-t2).get(),is(1));
  • max(Comparator<? super T>)
assertThat(of(1,2,3,4,5).max((t1,t2) -> t1-t2).get(),is(5));
  • count()
assertThat(of(1,5,3,4,2).count(),is(5L));
  • anyMatch(Predicate<? super T>)

Returns whether any elements of this stream match the provided predicate.

assertThat(of(1,2,3,4,5).anyMatch(it-> it.equals(3)),is(true));
  • allMatch(Predicate<? super T>)

Returns whether all elements of this stream match the provided predicate.

assertThat(of(1,2,3,4,5).allMatch(it-> it>0 && it <6),is(true));
  • noneMatch(Predicate<? super T>)

Returns whether no elements of this stream match the provided predicate.

assertThat(of(1,2,3,4,5).noneMatch(it-> it==5000),is(true));
  • findFirst()
assertThat(Arrays.asList(1,2,3),
hasItem(of(1,2,3,4,5).filter(it -> it <3)
.findFirst()
.get()));
  • findAny()
assertThat(Arrays.asList(1,2,3),
hasItem(of(1,2,3,4,5).filter(it -> it <3)
.findAny()
.get()));

The Tutorial : Reactive programming with Java 8

--

--

John McClean

Architecture @ Verizon Media. Maintainer of Cyclops. Twitter @johnmcclean_ie