2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com>
|
2018-06-30 05:10:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package jdocs.stream.operators;
|
|
|
|
|
|
2019-11-18 18:02:44 +02:00
|
|
|
import akka.actor.ActorSystem;
|
2019-09-19 11:22:37 +02:00
|
|
|
import akka.japi.pf.PFBuilder;
|
2018-06-30 05:10:10 +02:00
|
|
|
import akka.stream.javadsl.Flow;
|
|
|
|
|
|
2018-10-25 17:07:48 +03:30
|
|
|
import akka.NotUsed;
|
|
|
|
|
import akka.japi.function.Function2;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #zip
|
|
|
|
|
// #zip-with
|
|
|
|
|
// #zip-with-index
|
|
|
|
|
// #or-else
|
|
|
|
|
// #prepend
|
|
|
|
|
// #concat
|
|
|
|
|
// #interleave
|
|
|
|
|
// #merge
|
|
|
|
|
// #merge-sorted
|
2018-10-02 18:26:26 +03:30
|
|
|
import akka.stream.javadsl.Source;
|
2018-10-25 17:07:48 +03:30
|
|
|
import akka.stream.javadsl.Sink;
|
2018-10-02 18:26:26 +03:30
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #merge-sorted
|
|
|
|
|
// #merge
|
|
|
|
|
// #interleave
|
|
|
|
|
// #concat
|
|
|
|
|
// #prepend
|
|
|
|
|
// #or-else
|
|
|
|
|
// #zip-with-index
|
|
|
|
|
// #zip-with
|
|
|
|
|
// #zip
|
|
|
|
|
|
|
|
|
|
// #log
|
2018-06-30 05:10:10 +02:00
|
|
|
import akka.stream.Attributes;
|
2019-09-10 13:24:24 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #log
|
2018-06-30 05:10:10 +02:00
|
|
|
|
2018-09-20 16:22:11 +09:00
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.Arrays;
|
2018-10-25 17:07:48 +03:30
|
|
|
import java.util.Comparator;
|
2019-11-26 10:57:12 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2019-11-26 11:25:20 +00:00
|
|
|
import java.util.concurrent.CompletionStage;
|
2018-09-20 16:22:11 +09:00
|
|
|
|
2018-06-30 05:10:10 +02:00
|
|
|
class SourceOrFlow {
|
2019-11-18 18:02:44 +02:00
|
|
|
private static ActorSystem system = null;
|
2018-06-30 05:10:10 +02:00
|
|
|
|
|
|
|
|
void logExample() {
|
|
|
|
|
Flow.of(String.class)
|
2019-01-12 04:00:53 +08:00
|
|
|
// #log
|
2018-09-20 16:22:11 +09:00
|
|
|
.log("myStream")
|
2019-01-12 04:00:53 +08:00
|
|
|
.addAttributes(
|
|
|
|
|
Attributes.createLogLevels(
|
|
|
|
|
Attributes.logLevelOff(), // onElement
|
2019-09-10 13:24:24 +02:00
|
|
|
Attributes.logLevelInfo(), // onFinish
|
|
|
|
|
Attributes.logLevelError())) // onFailure
|
2019-01-12 04:00:53 +08:00
|
|
|
// #log
|
2018-06-30 05:10:10 +02:00
|
|
|
;
|
|
|
|
|
}
|
2018-09-20 16:22:11 +09:00
|
|
|
|
2018-10-02 18:26:26 +03:30
|
|
|
void zipWithIndexExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #zip-with-index
|
2018-10-02 18:26:26 +03:30
|
|
|
Source.from(Arrays.asList("apple", "orange", "banana"))
|
2019-01-12 04:00:53 +08:00
|
|
|
.zipWithIndex()
|
2019-11-25 09:50:41 +00:00
|
|
|
.runWith(Sink.foreach(System.out::print), system);
|
2018-10-02 18:26:26 +03:30
|
|
|
// this will print ('apple', 0), ('orange', 1), ('banana', 2)
|
2019-01-12 04:00:53 +08:00
|
|
|
// #zip-with-index
|
2018-10-02 18:26:26 +03:30
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2018-10-25 17:07:48 +03:30
|
|
|
void zipExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #zip
|
2018-10-25 17:07:48 +03:30
|
|
|
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
|
|
|
|
|
Source<String, NotUsed> sourceFirstLetters = Source.from(Arrays.asList("A", "O", "B"));
|
2019-11-25 09:50:41 +00:00
|
|
|
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), system);
|
2018-10-25 17:07:48 +03:30
|
|
|
// this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B')
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #zip
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void zipWithExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #zip-with
|
2018-10-25 17:07:48 +03:30
|
|
|
Source<String, NotUsed> sourceCount = Source.from(Arrays.asList("one", "two", "three"));
|
|
|
|
|
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
|
2019-01-12 04:00:53 +08:00
|
|
|
sourceCount
|
|
|
|
|
.zipWith(
|
2018-10-25 17:07:48 +03:30
|
|
|
sourceFruits,
|
2019-01-12 04:00:53 +08:00
|
|
|
(Function2<String, String, String>) (countStr, fruitName) -> countStr + " " + fruitName)
|
2019-11-25 09:50:41 +00:00
|
|
|
.runWith(Sink.foreach(System.out::print), system);
|
2018-10-25 17:07:48 +03:30
|
|
|
// this will print 'one apple', 'two orange', 'three banana'
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #zip-with
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prependExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #prepend
|
|
|
|
|
Source<String, NotUsed> ladies = Source.from(Arrays.asList("Emma", "Emily"));
|
|
|
|
|
Source<String, NotUsed> gentlemen = Source.from(Arrays.asList("Liam", "William"));
|
2019-11-25 09:50:41 +00:00
|
|
|
gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// this will print "Emma", "Emily", "Liam", "William"
|
2018-10-25 17:07:48 +03:30
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #prepend
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void concatExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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));
|
2019-11-25 09:50:41 +00:00
|
|
|
sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// prints 1, 2, 3, 4, 10, 20, 30, 40
|
2018-10-25 17:07:48 +03:30
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #concat
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interleaveExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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));
|
2019-11-25 09:50:41 +00:00
|
|
|
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// prints 1, 2, 10, 20, 3, 4, 30, 40
|
2018-10-25 17:07:48 +03:30
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #interleave
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mergeExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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));
|
2019-11-25 09:50:41 +00:00
|
|
|
sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// merging is not deterministic, can for example print 1, 2, 3, 4, 10, 20, 30, 40
|
2018-10-25 17:07:48 +03:30
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #merge
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mergeSortedExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #merge-sorted
|
|
|
|
|
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 3, 5, 7));
|
|
|
|
|
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(2, 4, 6, 8));
|
|
|
|
|
sourceA
|
|
|
|
|
.mergeSorted(sourceB, Comparator.<Integer>naturalOrder())
|
2019-11-25 09:50:41 +00:00
|
|
|
.runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// 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())
|
2019-11-25 09:50:41 +00:00
|
|
|
.runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// prints 1, 3, 5, 7, 20, 1, 1, 1
|
|
|
|
|
// #merge-sorted
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void orElseExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #or-else
|
|
|
|
|
Source<String, NotUsed> source1 = Source.from(Arrays.asList("First source"));
|
|
|
|
|
Source<String, NotUsed> source2 = Source.from(Arrays.asList("Second source"));
|
|
|
|
|
Source<String, NotUsed> emptySource = Source.empty();
|
2018-10-25 17:07:48 +03:30
|
|
|
|
2019-11-25 09:50:41 +00:00
|
|
|
source1.orElse(source2).runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// this will print "First source"
|
2018-10-25 17:07:48 +03:30
|
|
|
|
2019-11-25 09:50:41 +00:00
|
|
|
emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// this will print "Second source"
|
2018-10-25 17:07:48 +03:30
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #or-else
|
2018-10-25 17:07:48 +03:30
|
|
|
}
|
|
|
|
|
|
2018-09-20 16:22:11 +09:00
|
|
|
void conflateExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #conflate
|
2018-09-20 16:22:11 +09:00
|
|
|
Source.cycle(() -> Arrays.asList(1, 10, 100).iterator())
|
|
|
|
|
.throttle(10, Duration.ofSeconds(1)) // fast upstream
|
|
|
|
|
.conflate((Integer acc, Integer el) -> acc + el)
|
|
|
|
|
.throttle(1, Duration.ofSeconds(1)); // slow downstream
|
2019-01-12 04:00:53 +08:00
|
|
|
// #conflate
|
2018-09-20 16:22:11 +09:00
|
|
|
}
|
|
|
|
|
|
2019-01-04 22:08:54 +08:00
|
|
|
void scanExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #scan
|
2019-01-04 22:08:54 +08:00
|
|
|
Source<Integer, NotUsed> source = Source.range(1, 5);
|
2019-11-25 09:50:41 +00:00
|
|
|
source.scan(0, (acc, x) -> acc + x).runForeach(System.out::println, system);
|
2019-01-04 22:08:54 +08:00
|
|
|
// 0 (= 0)
|
|
|
|
|
// 1 (= 0 + 1)
|
|
|
|
|
// 3 (= 0 + 1 + 2)
|
|
|
|
|
// 6 (= 0 + 1 + 2 + 3)
|
|
|
|
|
// 10 (= 0 + 1 + 2 + 3 + 4)
|
|
|
|
|
// 15 (= 0 + 1 + 2 + 3 + 4 + 5)
|
2019-01-12 04:00:53 +08:00
|
|
|
// #scan
|
2019-01-04 22:08:54 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-26 11:25:20 +00:00
|
|
|
// #scan-async
|
|
|
|
|
CompletionStage<Integer> asyncFunction(int acc, int next) {
|
|
|
|
|
return CompletableFuture.supplyAsync(() -> acc + next);
|
|
|
|
|
}
|
|
|
|
|
// #scan-async
|
|
|
|
|
|
2019-11-26 10:57:12 +01:00
|
|
|
void scanAsyncExample() {
|
2019-11-26 11:25:20 +00:00
|
|
|
// #scan-async
|
2019-11-26 10:57:12 +01:00
|
|
|
Source<Integer, NotUsed> source = Source.range(1, 5);
|
2019-11-26 11:25:20 +00:00
|
|
|
source.scanAsync(0, (acc, x) -> asyncFunction(acc, x)).runForeach(System.out::println, system);
|
2019-11-26 10:57:12 +01:00
|
|
|
// 0 (= 0)
|
|
|
|
|
// 1 (= 0 + 1)
|
|
|
|
|
// 3 (= 0 + 1 + 2)
|
|
|
|
|
// 6 (= 0 + 1 + 2 + 3)
|
|
|
|
|
// 10 (= 0 + 1 + 2 + 3 + 4)
|
|
|
|
|
// 15 (= 0 + 1 + 2 + 3 + 4 + 5)
|
2019-11-26 11:25:20 +00:00
|
|
|
// #scan-async
|
2019-11-26 10:57:12 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
static // #conflateWithSeed-type
|
2018-09-20 16:22:11 +09:00
|
|
|
class Summed {
|
|
|
|
|
|
|
|
|
|
private final Integer el;
|
|
|
|
|
|
|
|
|
|
public Summed(Integer el) {
|
|
|
|
|
this.el = el;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Summed sum(Summed other) {
|
|
|
|
|
return new Summed(this.el + other.el);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #conflateWithSeed-type
|
2018-09-20 16:22:11 +09:00
|
|
|
|
|
|
|
|
void conflateWithSeedExample() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #conflateWithSeed
|
2018-09-20 16:22:11 +09:00
|
|
|
|
|
|
|
|
Source.cycle(() -> Arrays.asList(1, 10, 100).iterator())
|
|
|
|
|
.throttle(10, Duration.ofSeconds(1)) // fast upstream
|
|
|
|
|
.conflateWithSeed(Summed::new, (Summed acc, Integer el) -> acc.sum(new Summed(el)))
|
|
|
|
|
.throttle(1, Duration.ofSeconds(1)); // slow downstream
|
2019-01-12 04:00:53 +08:00
|
|
|
// #conflateWithSeed
|
2018-09-20 16:22:11 +09:00
|
|
|
}
|
2019-09-19 11:22:37 +02:00
|
|
|
|
|
|
|
|
// #collect-elements
|
|
|
|
|
static interface Message {}
|
|
|
|
|
|
|
|
|
|
static class Ping implements Message {
|
|
|
|
|
final int id;
|
|
|
|
|
|
|
|
|
|
Ping(int id) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class Pong {
|
|
|
|
|
final int id;
|
|
|
|
|
|
|
|
|
|
Pong(int id) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// #collect-elements
|
|
|
|
|
|
|
|
|
|
void collectExample() {
|
|
|
|
|
// #collect
|
|
|
|
|
Flow<Message, Pong, NotUsed> flow =
|
|
|
|
|
Flow.of(Message.class)
|
|
|
|
|
.collect(
|
|
|
|
|
new PFBuilder<Message, Pong>()
|
|
|
|
|
.match(Ping.class, p -> p.id != 0, p -> new Pong(p.id))
|
|
|
|
|
.build());
|
|
|
|
|
// #collect
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void collectTypeExample() {
|
|
|
|
|
// #collectType
|
|
|
|
|
Flow<Message, Pong, NotUsed> flow =
|
|
|
|
|
Flow.of(Message.class)
|
|
|
|
|
.collectType(Ping.class)
|
|
|
|
|
.filter(p -> p.id != 0)
|
|
|
|
|
.map(p -> new Pong(p.id));
|
|
|
|
|
// #collectType
|
|
|
|
|
}
|
2019-10-18 08:51:31 +02:00
|
|
|
|
|
|
|
|
void groupedExample() {
|
|
|
|
|
// #grouped
|
|
|
|
|
Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7))
|
|
|
|
|
.grouped(3)
|
2019-11-25 09:50:41 +00:00
|
|
|
.runForeach(System.out::println, system);
|
2019-10-18 08:51:31 +02:00
|
|
|
// [1, 2, 3]
|
|
|
|
|
// [4, 5, 6]
|
|
|
|
|
// [7]
|
|
|
|
|
|
|
|
|
|
Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7))
|
|
|
|
|
.grouped(3)
|
|
|
|
|
.map(g -> g.stream().reduce(0, Integer::sum))
|
2019-11-25 09:50:41 +00:00
|
|
|
.runForeach(System.out::println, system);
|
2019-10-18 08:51:31 +02:00
|
|
|
// 6 (= 1 + 2 + 3)
|
|
|
|
|
// 15 (= 4 + 5 + 6)
|
|
|
|
|
// 7 (= 7)
|
|
|
|
|
// #grouped
|
|
|
|
|
}
|
2019-11-18 18:02:44 +02:00
|
|
|
|
|
|
|
|
static
|
|
|
|
|
// #fold
|
|
|
|
|
class Histogram {
|
|
|
|
|
final long low;
|
|
|
|
|
final long high;
|
|
|
|
|
|
|
|
|
|
private Histogram(long low, long high) {
|
|
|
|
|
this.low = low;
|
|
|
|
|
this.high = high;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Immutable start value
|
|
|
|
|
public static Histogram INSTANCE = new Histogram(0L, 0L);
|
|
|
|
|
|
|
|
|
|
public Histogram add(int number) {
|
|
|
|
|
if (number < 100) {
|
|
|
|
|
return new Histogram(low + 1L, high);
|
|
|
|
|
} else {
|
|
|
|
|
return new Histogram(low, high + 1L);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// #fold
|
|
|
|
|
|
|
|
|
|
void foldExample() {
|
|
|
|
|
// #fold
|
|
|
|
|
|
|
|
|
|
// Folding over the numbers from 1 to 150:
|
|
|
|
|
Source.range(1, 150)
|
|
|
|
|
.fold(Histogram.INSTANCE, (acc, n) -> acc.add(n))
|
|
|
|
|
.runForeach(h -> System.out.println("Histogram(" + h.low + ", " + h.high + ")"), system);
|
|
|
|
|
|
|
|
|
|
// Prints: Histogram(99, 51)
|
|
|
|
|
// #fold
|
|
|
|
|
}
|
2019-11-25 09:50:41 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2019-11-26 15:54:42 +01:00
|
|
|
|
|
|
|
|
void filterExample() {
|
|
|
|
|
// #filter
|
|
|
|
|
Source<String, NotUsed> words =
|
|
|
|
|
Source.from(
|
|
|
|
|
Arrays.asList(
|
|
|
|
|
("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
|
|
|
|
|
+ "ut labore et dolore magna aliqua.")
|
|
|
|
|
.split(" ")));
|
|
|
|
|
|
|
|
|
|
Source<String, NotUsed> longWords = words.filter(w -> w.length() > 6);
|
|
|
|
|
|
|
|
|
|
longWords.runWith(Sink.foreach(System.out::print), system);
|
|
|
|
|
// consectetur
|
|
|
|
|
// adipiscing
|
|
|
|
|
// eiusmod
|
|
|
|
|
// tempor
|
|
|
|
|
// incididunt
|
|
|
|
|
// #filter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void filterNotExample() {
|
|
|
|
|
// #filterNot
|
|
|
|
|
Source<String, NotUsed> words =
|
|
|
|
|
Source.from(
|
|
|
|
|
Arrays.asList(
|
|
|
|
|
("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
|
|
|
|
|
+ "ut labore et dolore magna aliqua.")
|
|
|
|
|
.split(" ")));
|
|
|
|
|
|
|
|
|
|
Source<String, NotUsed> longWords = words.filterNot(w -> w.length() <= 5);
|
|
|
|
|
|
|
|
|
|
longWords.runWith(Sink.foreach(System.out::print), system);
|
|
|
|
|
// consectetur
|
|
|
|
|
// adipiscing
|
|
|
|
|
// eiusmod
|
|
|
|
|
// tempor
|
|
|
|
|
// incididunt
|
|
|
|
|
// #filterNot
|
|
|
|
|
}
|
2018-06-30 05:10:10 +02:00
|
|
|
}
|