Skip to content

Asynchronous terminal operations

johnmcclean-aol edited this page Nov 20, 2016 · 3 revisions

FutureOperations

Available on both ReactiveSeq (single threaded execution) and LazyFutureStream (parallel / concurrent execution)

Terminal operations can now (since v0.99) all be called asynchronously (with and without parallel reduction if necessary) for both LazyFutureStream and EagerFutureStream. e.g.

        CompletableFuture<Integer> size = LazyFutureStream.of(1,2,3,4)
                                                          .futureOperations()
                                                          .count();

Available operations

  • public CompletableFuture<List<T>> toList()

Asynchronously perform a mutable reduction to a JDK List

 CompletableFuture<List<Data>> myList = EagerFutureStream.of(1,2,3,4)
	   												.map(this::loadFromDb)
												       .withTaskExecutor(parallelBuilder().getExecutor())
	   												.map(this::processOnDifferentExecutor)
	   												.toList();
  • public CompletableFuture<Set<T>> toSet()

Asynchronously perform a mutable reduction to a JDK Set

CompletableFuture<Set<Data>> myList = LazyFutureStream.of(1,2,3,4)
	   														.map(this::loadFromDb)
	   														.withTaskExecutor(parallelBuilder().getExecutor())
	   														.map(this::processOnDifferentExecutor)
	   														.toSet();
  • public <U extends Comparable<U>> CompletableFuture<Optional<T>> minBy(Function<T, U> function) Asynchronously capture the minimum value in this stream using the provided function
    CompletableFuture<Optional<Integer>> min =  LazyFutureStream.of(1, 2, 3, 4, 5, 6)
                                                                      .futureOperations()    
                                                                      .minBy(t -> Math.abs(t - 5));
    //min CompletableFuture[Optional[5]]  //5-5 =0
  • public <U extends Comparable<U>> CompletableFuture<Optional<T>> maxBy(Function<T, U> function) Asynchronously capture the maximum value in this stream using the provided function
CompletableFuture<Optional<Integer>> max =  LazyFutureStream.of(1, 2, 3, 4, 5, 6)
                                                                      .futureOperations()            
                                                                      .maxBy(t -> Math.abs(t - 5));
    //min CompletableFuture[Optional[1]]  //Math.abs(1-5) =4
  • public <R, A> CompletableFuture<R> collect(Collector<? super T, A, R> collector) Asynchronously perform a Stream collection
CompletableFuture<List<Integer>> list = LazyFutureStream.of(1,2,3,4,5)
                                      .futureOperations()
                                      .collect(Collectors.toList());

//CompletableFuture[1,2,3,4,5] 
  • public CompletableFuture<Optional<T>> reduce(BinaryOperator<T> accumulator)
CompletableFuture<Optional<Integer>> sum = LazyFutureStream.of(1,2,3,4,5)
                              .map(it -> it*100).futureOperations()
                              .reduce( (acc,next) -> acc+next)
  • public <A> CompletableFuture<A[]> toArray(IntFunction<A[]> generator)
CompletableFuture<Integer[]> array = LazyFutureStream.of(1,5,3,4,2).futureOperations()
				.toArray(it->new Integer[it]);
  • public CompletableFuture<Object[]> toArray()
CompletableFuture<Integer[]> array = LazyFutureStream.of(1,5,3,4,2).futureOperations()
							.toArray()
  • public <K> CompletableFuture<Map<K, List<T>>> groupBy(Function<? super T, ? extends K> classifier)
Map<Integer, List<Integer>> map1 = LazyFutureStream.of(1, 2, 3, 4)
	        										.futureOperations()
	        										.groupBy(i -> i % 2)
	        										.join()
  • public <K, A, D> CompletableFuture<Map<K, D>> groupBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
Map<Integer, Long> map3 =
				       LazyFutureStream.of(tuple(1, 1), tuple(1, 2), tuple(1, 3), tuple(2, 1), tuple(2, 2))
				           .futureOperations()
				       	   .groupBy(t -> t.v1, counting()).join();
  • public <K, D, A, M extends Map<K, D>> CompletableFuture<M> groupBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
Map<Integer, Tuple2<Long, String>> map4 =
			        of(tuple(1, 1), tuple(1, 2), tuple(1, 3), tuple(2, 4), tuple(2, 5))
			          .futureOperations()
			           .groupBy(t -> t.v1, collectors(counting(), 
			        		   mapping(t -> ((Tuple2<Integer,Integer>)t).map2(Object::toString).v2,
			        				   joining(", ")))).join();
  • public <U> CompletableFuture<U> foldLeft(U seed, BiFunction<U, ? super T, U> function)
CompletableFuture<String> concat = LazyFutureStream.of("a", "b", "c")
                                                         .futureOperations()
                                                         .foldLeft("", String::concat).join()
  • public <U> CompletableFuture<U> foldRight(U seed, BiFunction<? super T, U, U> function)
CompletableFuture<String> reveresed = LazyFutureStream.of("a", "b", "c")
                             .futureOperations()
                             .foldRight("", String::concat);

//cba
  • public CompletableFuture<Optional<T>> min(Comparator<? super T> comparator)
CompletableFuture<Optional<Integer>> min = LazyFutureStream.of(1,2,3,4,5)
                             .futureOperations()
                             .min((t1,t2) -> t1-t2);

//1
				
  • public CompletableFuture<Optional<T>> max(Comparator<? super T> comparator)
CompletableFuture<Optional<Integer>> min = LazyFutureStream.of(1,2,3,4,5)
                             .futureOperations()
                             .max((t1,t2) -> t1-t2);

//5

public <C extends Collection<T>> CompletableFuture<C> toCollection(Supplier<C> collectionFactory)

CompletableFuture<Collection<Integer>> col =  LazyFutureStream.of(1,5,3,4,2).futureOperations()
				.toCollection(()->new ArrayList())
  • public <R> CompletableFuture<R> collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner)
  • public <U> CompletableFuture<U> reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner)
CompletableFuture<Integer> sum = LazyFutureStream.of(1,2,3,4,5).map(it -> it*100)
				.futureOperations().reduce( 0,
                (acc, next) -> acc+next,
                Integer::sum);

//1550
  • public CompletableFuture<T> reduce(T identity, BinaryOperator<T> accumulator)
CompletableFuture<Integer> sum = LazyFutureStream.of(1,2,3,4,5).map(it -> it*100)
				.futureOperations()
				.reduce( 50,(acc,next) -> acc+next);

//1550
  • public CompletableFuture<Long> count()
CompletableFuture<Integer> sum = LazyFutureStream.of(1,2,3,4,5).map(it -> it*100)
				.futureOperations()
				.count();
//5
  • public CompletableFuture<String> join(CharSequence sep)
CompletableFuture<String> concat = LazyFutureStream.of(1, 2, 3)
                              .futureOperations()
                              .join(", ");
//"1,2,3"
  • public CompletableFuture<String> join()
CompletableFuture<String> concat = LazyFutureStream.of(1, 2, 3)
                              .futureOperations()
                              .join();
//"123"
  • public CompletableFuture<String> join(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
CompletableFuture<String> concat = LazyFutureStream.of(1, 2, 3)
                              .futureOperations()
                              .join(", ","!","?");
//"!1,2,3?"
  • public CompletableFuture<Optional<T>> findAny()
CompletableFuture<Optional<Integer>> any = LazyFutureStream.of(1,2,3,4,5)
                              .filter(it -> it <3)
                              .futureOperations()
                              .findAny()
//Optional[1]
  • public CompletableFuture<Optional<T>> findFirst()
CompletableFuture<Optional<Integer>> first = LazyFutureStream.of(1,2,3,4,5)
                              .filter(it -> it <3)
                              .futureOperations()
                              .findFirst()
//Optional[1]
  • public CompletableFuture<T> firstValue()
CompletableFuture<Integer> first = LazyFutureStream.of(1,2,3,4,5)
                              .filter(it -> it <3)
                              .futureOperations()
                              .firstValue()
//1
  • public CompletableFuture<Boolean> allMatch(Predicate<? super T> predicate)
CompletableFuture<Boolean> match = LazyFutureStream.of(1,2,3,4,5)
                              .futureOperations()
                              .allMatch(it-> it>0 && it <6);
//true
  • public CompletableFuture<Boolean> anyMatch(Predicate<? super T> predicate)
CompletableFuture<Boolean> match = LazyFutureStream.of(1,2,3,4,5)
                              .futureOperations()
                              .anyMatch(it-> it.equals(3));
//true
  • public CompletableFuture<Boolean> noneMatch(Predicate<? super T> predicate)
CompletableFuture<Boolean> match = LazyFutureStream.of(1,2,3,4,5)
                              .futureOperations()
                              .noneMatch(it-> it==5000);
//true
  • CompletableFuture<Integer> sumInt(ToIntFunction<T> fn)
CompletableFuture<Integer> sum = LazyFutureStream.of(1,2,3,4)
                                     .futureOperations(exec)
                                     .sumInt(i->i);
//10
  • CompletableFuture<OptionalInt> maxInt(ToIntFunction<T> fn)
CompletableFuture<OptionalInt> max = LazyFutureStream.of(1,2,3,4)
                                     .futureOperations(exec)
                                     .maxInt(i->i);
//4
  • CompletableFuture<OptionalInt> minInt(ToIntFunction<T> fn)
CompletableFuture<OptionalInt> min = LazyFutureStream.of(1,2,3,4)
                                     .futureOperations(exec)
                                     .minInt(i->i);
//1
  • CompletableFuture<OptionalDouble> averageInt(ToIntFunction<T> fn)
CompletableFuture<OptionalDouble> avg = LazyFutureStream.of(1,2,3,4)
                                      .futureOperations(exec)
                                     .averageInt(i->i);
//2.5
  • CompletableFuture<IntSummaryStatistics> summaryStatisticsInt(ToIntFunction<T> fn)
CompletableFuture<IntSummaryStatistics> avg = LazyFutureStream.of(1,2,3,4)
                                     .futureOperations(exec)
                                     .summaryStatisticsInt(i->i);
  • CompletableFuture<Long> sumLong(ToLongFunction<T> fn)
CompletableFuture<Long> sum = LazyFutureStream.of(1l,2l,3l,4l)
                                     .futureOperations(exec)
                                     .sumLong(i->i);
//10l
  • CompletableFuture<OptionalLong> maxLong(ToLongFunction<T> fn)
CompletableFuture<OptionalLong> max = LazyFutureStream.of(1l,2l,3l,4l)
                                     .futureOperations(exec)
                                     .maxLong(i->i);
//4l
  • CompletableFuture<OptionalLong> minLong(ToLongFunction<T> fn)
CompletableFuture<OptionalLong> min = LazyFutureStream.of(1l,2l,3l,4l)
                                     .futureOperations(exec)
                                     .minLong(i->i);
//1l
  • CompletableFuture<OptionalDouble> averageLong(ToLongFunction<T> fn)
CompletableFuture<OptionalDouble> avg = LazyFutureStream.of(1l,2l,3l,4l)
                                     .futureOperations(exec)
                                     .averageLong(i->i);
//2.5
  • CompletableFuture<LongSummaryStatistics> summaryStatisticsLong(ToLongFunction<T> fn)
CompletableFuture<LongSummaryStatistics> avg = LazyFutureStream.of(1,2,3,4)
                                     .futureOperations(exec)
                                     .summaryStatisticsLong(i->i);
  • CompletableFuture<Double> sumDouble(ToDoubleFunction<T> fn)
CompletableFuture<Double> sum = LazyFutureStream.of(1d,2d,3d,4d)
                                     .futureOperations(exec)
                                     .sumDouble(i->i);
//10d
  • CompletableFuture<OptionalDouble> maxDouble(ToDoubleFunction<T> fn)
CompletableFuture<OptionalDouble> max = LazyFutureStream.of(1d,2d,3d,4d)
                                      .futureOperations(exec)
                                      .maxInt(i->i);
//4d
  • CompletableFuture<OptionalDouble> minDouble(ToDoubleFunction<T> fn)
CompletableFuture<OptionalDouble> min = LazyFutureStream.of(1d,2d,3d,4d)
                                     .futureOperations(exec)
                                     .minDouble(i->i);
//1d
  • CompletableFuture<OptionalDouble> averageDouble(ToDoubleFunction<T> fn)
CompletableFuture<OptionalDouble> avg = LazyFutureStream.of(1d,2d,3d,4d)
                                     .futureOperations(exec)
                                     .averageDouble(i->i);
//2.5
  • CompletableFuture<DoubleSummaryStatistics> summaryStatisticsDouble(ToDoubleFunction<T> fn)
CompletableFuture<DoubleSummaryStatistics> avg = LazyFutureStream.of(1,2,3,4)
                                     .futureOperations(exec)
                                     .summaryStatisticsDouble(i->i);
Clone this wiki locally