=doc: take and takeWhile (#28078)

* Switch java examples to pass in actorsystem instead of materializer
This commit is contained in:
Christopher Batey 2019-11-25 09:50:41 +00:00 committed by Patrik Nordwall
parent 0384baa53e
commit 1b68951f86
5 changed files with 93 additions and 17 deletions

View file

@ -6,7 +6,6 @@ package jdocs.stream.operators;
import akka.actor.ActorSystem;
import akka.japi.pf.PFBuilder;
import akka.stream.Materializer;
import akka.stream.javadsl.Flow;
import akka.NotUsed;
@ -45,7 +44,6 @@ import java.util.Arrays;
import java.util.Comparator;
class SourceOrFlow {
private static Materializer materializer = null;
private static ActorSystem system = null;
void logExample() {
@ -62,11 +60,10 @@ class SourceOrFlow {
}
void zipWithIndexExample() {
Materializer materializer = null;
// #zip-with-index
Source.from(Arrays.asList("apple", "orange", "banana"))
.zipWithIndex()
.runWith(Sink.foreach(System.out::print), materializer);
.runWith(Sink.foreach(System.out::print), system);
// this will print ('apple', 0), ('orange', 1), ('banana', 2)
// #zip-with-index
}
@ -75,7 +72,7 @@ class SourceOrFlow {
// #zip
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
Source<String, NotUsed> sourceFirstLetters = Source.from(Arrays.asList("A", "O", "B"));
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), materializer);
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), system);
// this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B')
// #zip
@ -89,7 +86,7 @@ class SourceOrFlow {
.zipWith(
sourceFruits,
(Function2<String, String, String>) (countStr, fruitName) -> countStr + " " + fruitName)
.runWith(Sink.foreach(System.out::print), materializer);
.runWith(Sink.foreach(System.out::print), system);
// this will print 'one apple', 'two orange', 'three banana'
// #zip-with
@ -99,7 +96,7 @@ class SourceOrFlow {
// #prepend
Source<String, NotUsed> ladies = Source.from(Arrays.asList("Emma", "Emily"));
Source<String, NotUsed> gentlemen = Source.from(Arrays.asList("Liam", "William"));
gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), materializer);
gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), system);
// this will print "Emma", "Emily", "Liam", "William"
// #prepend
@ -109,7 +106,7 @@ class SourceOrFlow {
// #concat
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), materializer);
sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), system);
// prints 1, 2, 3, 4, 10, 20, 30, 40
// #concat
@ -119,7 +116,7 @@ class SourceOrFlow {
// #interleave
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), materializer);
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), system);
// prints 1, 2, 10, 20, 3, 4, 30, 40
// #interleave
@ -129,7 +126,7 @@ class SourceOrFlow {
// #merge
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), materializer);
sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), system);
// merging is not deterministic, can for example print 1, 2, 3, 4, 10, 20, 30, 40
// #merge
@ -141,13 +138,13 @@ class SourceOrFlow {
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(2, 4, 6, 8));
sourceA
.mergeSorted(sourceB, Comparator.<Integer>naturalOrder())
.runWith(Sink.foreach(System.out::print), materializer);
.runWith(Sink.foreach(System.out::print), system);
// prints 1, 2, 3, 4, 5, 6, 7, 8
Source<Integer, NotUsed> sourceC = Source.from(Arrays.asList(20, 1, 1, 1));
sourceA
.mergeSorted(sourceC, Comparator.<Integer>naturalOrder())
.runWith(Sink.foreach(System.out::print), materializer);
.runWith(Sink.foreach(System.out::print), system);
// prints 1, 3, 5, 7, 20, 1, 1, 1
// #merge-sorted
}
@ -158,10 +155,10 @@ class SourceOrFlow {
Source<String, NotUsed> source2 = Source.from(Arrays.asList("Second source"));
Source<String, NotUsed> emptySource = Source.empty();
source1.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
source1.orElse(source2).runWith(Sink.foreach(System.out::print), system);
// this will print "First source"
emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), system);
// this will print "Second source"
// #or-else
@ -179,7 +176,7 @@ class SourceOrFlow {
void scanExample() {
// #scan
Source<Integer, NotUsed> source = Source.range(1, 5);
source.scan(0, (acc, x) -> acc + x).runForeach(System.out::println, materializer);
source.scan(0, (acc, x) -> acc + x).runForeach(System.out::println, system);
// 0 (= 0)
// 1 (= 0 + 1)
// 3 (= 0 + 1 + 2)
@ -259,7 +256,7 @@ class SourceOrFlow {
// #grouped
Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7))
.grouped(3)
.runForeach(System.out::println, materializer);
.runForeach(System.out::println, system);
// [1, 2, 3]
// [4, 5, 6]
// [7]
@ -267,7 +264,7 @@ class SourceOrFlow {
Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7))
.grouped(3)
.map(g -> g.stream().reduce(0, Integer::sum))
.runForeach(System.out::println, materializer);
.runForeach(System.out::println, system);
// 6 (= 1 + 2 + 3)
// 15 (= 4 + 5 + 6)
// 7 (= 7)
@ -309,4 +306,25 @@ class SourceOrFlow {
// Prints: Histogram(99, 51)
// #fold
}
void takeExample() {
// #take
Source.from(Arrays.asList(1, 2, 3, 4, 5)).take(3).runForeach(System.out::println, system);
// this will print:
// 1
// 2
// 3
// #take
}
void takeWhileExample() {
// #take-while
Source.from(Arrays.asList(1, 2, 3, 4, 5))
.takeWhile(i -> i < 3)
.runForeach(System.out::println, system);
// this will print:
// 1
// 2
// #take-while
}
}