Adding example for collection operator (#29716)

* Adding example for collection operator

* Modifying example

* Add parenthesises to side effect only method

Co-authored-by: Johan Andrén <johan@markatta.com>
This commit is contained in:
Muskan Gupta 2020-10-12 14:38:57 +05:30 committed by GitHub
parent 4620c64dc6
commit 0e0680bd82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -13,6 +13,13 @@
@scala[Collect values emitted from the stream into an arbitrary collection `That`. The resulting collection is available through a `Future[That]` or when the stream completes. Note that the collection boundaries are those defined in the `CanBuildFrom` associated with the chosen collection. See [The Architecture of Scala 2.13's Collections](https://docs.scala-lang.org/overviews/core/architecture-of-scala-213-collections.html) for more info. The [`seq`](seq.html) operator is a shorthand for `Sink.collection[T, Vector[T]]`.]@java[Operator only available in the Scala API. The closest operator in the Java API is [`Sink.seq`](seq.html).]
## Example
This example reads the numbers from a source and stores them in the List collection.
Scala
: @@snip [Collection.scala](/akka-docs/src/test/scala/docs/stream/operators/sink/Collection.scala) { #collection }
## Reactive Streams semantics
@@@

View file

@ -0,0 +1,23 @@
/*
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.stream.operators.sink
import akka.actor.ActorSystem
import akka.stream.scaladsl.{ Sink, Source }
import scala.concurrent.{ ExecutionContextExecutor, Future }
object Collection {
implicit val system: ActorSystem = ???
implicit val ec: ExecutionContextExecutor = system.dispatcher
def collectionExample(): Unit = {
//#collection
val source = Source(1 to 5)
val result: Future[List[Int]] = source.runWith(Sink.collection[Int, List[Int]])
result.foreach(println)
//List(1, 2, 3, 4, 5)
//#collection
}
}