Examples for zipN, zipWithN and zipAll (#28510)
This commit is contained in:
parent
a614f0bee7
commit
8d8fa29f47
9 changed files with 234 additions and 25 deletions
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.stream.operators.source;
|
||||
|
||||
import akka.NotUsed;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.stream.javadsl.Source;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Zip {
|
||||
|
||||
void zipNSample() {
|
||||
ActorSystem system = null;
|
||||
|
||||
// #zipN-simple
|
||||
Source<Object, NotUsed> chars = Source.from(Arrays.asList("a", "b", "c", "e", "f"));
|
||||
Source<Object, NotUsed> numbers = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
Source<Object, NotUsed> colors =
|
||||
Source.from(Arrays.asList("red", "green", "blue", "yellow", "purple"));
|
||||
|
||||
Source.zipN(Arrays.asList(chars, numbers, colors)).runForeach(System.out::println, system);
|
||||
// prints:
|
||||
// [a, 1, red]
|
||||
// [b, 2, green]
|
||||
// [c, 3, blue]
|
||||
// [e, 4, yellow]
|
||||
// [f, 5, purple]
|
||||
|
||||
// #zipN-simple
|
||||
}
|
||||
|
||||
void zipWithNSample() {
|
||||
ActorSystem system = null;
|
||||
|
||||
// #zipWithN-simple
|
||||
Source<Integer, NotUsed> numbers = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6));
|
||||
Source<Integer, NotUsed> otherNumbers = Source.from(Arrays.asList(5, 2, 1, 4, 10, 4));
|
||||
Source<Integer, NotUsed> andSomeOtherNumbers = Source.from(Arrays.asList(3, 7, 2, 1, 1));
|
||||
|
||||
Source.zipWithN(
|
||||
(List<Integer> seq) -> seq.stream().mapToInt(i -> i).max().getAsInt(),
|
||||
Arrays.asList(numbers, otherNumbers, andSomeOtherNumbers))
|
||||
.runForeach(System.out::println, system);
|
||||
// prints:
|
||||
// 5
|
||||
// 7
|
||||
// 3
|
||||
// 4
|
||||
// 10
|
||||
|
||||
// #zipWithN-simple
|
||||
}
|
||||
|
||||
void zipAllSample() {
|
||||
ActorSystem system = null;
|
||||
// #zipAll-simple
|
||||
|
||||
Source<Integer, NotUsed> numbers = Source.from(Arrays.asList(1, 2, 3, 4));
|
||||
Source<String, NotUsed> letters = Source.from(Arrays.asList("a", "b", "c"));
|
||||
|
||||
numbers.zipAll(letters, -1, "default").runForeach(System.out::println, system);
|
||||
// prints:
|
||||
// Pair(1,a)
|
||||
// Pair(2,b)
|
||||
// Pair(3,c)
|
||||
// Pair(4,default)
|
||||
// #zipAll-simple
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators.source
|
||||
|
||||
import akka.NotUsed
|
||||
import akka.actor.typed.ActorSystem
|
||||
import akka.stream.scaladsl.Source
|
||||
|
||||
object Zip {
|
||||
|
||||
implicit val system: ActorSystem[_] = ???
|
||||
|
||||
def zipN(): Unit = {
|
||||
// #zipN-simple
|
||||
val chars = Source("a" :: "b" :: "c" :: "e" :: "f" :: Nil)
|
||||
val numbers = Source(1 :: 2 :: 3 :: 4 :: 5 :: 6 :: Nil)
|
||||
val colors = Source("red" :: "green" :: "blue" :: "yellow" :: "purple" :: Nil)
|
||||
|
||||
Source.zipN(chars :: numbers :: colors :: Nil).runForeach(println)
|
||||
// prints:
|
||||
// Vector(a, 1, red)
|
||||
// Vector(b, 2, green)
|
||||
// Vector(c, 3, blue)
|
||||
// Vector(e, 4, yellow)
|
||||
// Vector(f, 5, purple)
|
||||
|
||||
// #zipN-simple
|
||||
}
|
||||
|
||||
def zipNWith(): Unit = {
|
||||
// #zipWithN-simple
|
||||
val numbers = Source(1 :: 2 :: 3 :: 4 :: 5 :: 6 :: Nil)
|
||||
val otherNumbers = Source(5 :: 2 :: 1 :: 4 :: 10 :: 4 :: Nil)
|
||||
val andSomeOtherNumbers = Source(3 :: 7 :: 2 :: 1 :: 1 :: Nil)
|
||||
|
||||
Source
|
||||
.zipWithN((seq: Seq[Int]) => seq.max)(numbers :: otherNumbers :: andSomeOtherNumbers :: Nil)
|
||||
.runForeach(println)
|
||||
// prints:
|
||||
// 5
|
||||
// 7
|
||||
// 3
|
||||
// 4
|
||||
// 10
|
||||
|
||||
// #zipWithN-simple
|
||||
}
|
||||
|
||||
def zipAll() {
|
||||
// #zipAll-simple
|
||||
val numbers = Source(1 :: 2 :: 3 :: 4 :: Nil)
|
||||
val letters = Source("a" :: "b" :: "c" :: Nil)
|
||||
|
||||
numbers.zipAll(letters, -1, "default").runForeach(println)
|
||||
// prints:
|
||||
// (1,a)
|
||||
// (2,b)
|
||||
// (3,c)
|
||||
// (4,default)
|
||||
// #zipAll-simple
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue