Skip to content

Commit d3606b7

Browse files
committed
Describe how to get a Stream from Scala colls in Java.
Also fixed the Scala example to actually run.
1 parent af5de57 commit d3606b7

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ obtain Java 8 Streams from Scala collections from within Java.
143143

144144
For sequential operations, Scala's `iterator` almost always equals or exceeds the performance of a Java 8 stream. Thus,
145145
one should favor `iterator` (and its richer set of operations) over `seqStream` for general use. However, long
146-
chains of processing of primitive types can sometimes benefit from the manually specialized methods in `DoubleStream`,
146+
chains of processing of primitive types can sometimes benefit from the manually specialized methods in `DoubleStream`,
147147
`IntStream`, and `LongStream`.
148148

149149
Note that although `iterator` typically has superior performance in a sequential context, the advantage is modest
@@ -173,13 +173,14 @@ object Test {
173173
val m = collection.immutable.HashMap("fish" -> 2, "bird" -> 4)
174174
val s = m.parValueStream.sum // 6, potientially computed in parallel
175175
val t = m.seqKeyStream.toScala[List] // List("fish", "bird")
176-
val a = t.accumulate // Accumulator[(String, Int)]
176+
val a = m.accumulate // Accumulator[(String, Int)]
177177

178-
val n = a.splitter.fold(0)(_ + _._1.length) +
178+
val n = a.stepper.fold(0)(_ + _._1.length) +
179179
a.parStream.count // 8 + 2 = 10
180180

181181
val b = java.util.Arrays.stream(Array(2L, 3L, 4L)).
182182
accumulate // LongAccumulator
183+
val l = b.to[List] // List(2L, 3L, 4L)
183184
}
184185
```
185186

@@ -216,6 +217,34 @@ def mapToSortedString[A](xs: Vector[A], f: A => String, sep: String) =
216217

217218
#### Java Usage Example
218219

220+
To convert a Scala collection to a Java 8 Stream from within Java, it usually
221+
suffices to call `ScalaStreaming.from(xs)` on your collection `xs`. If `xs` is
222+
a map, you may wish to get the keys or values alone by using `fromKeys` or
223+
`fromValues`. If the collection has an underlying representation that is not
224+
efficiently parallelized (e.g. `scala.collection.immutable.List`), then
225+
`fromAccumulated` (and `fromAccumulatedKeys` and `fromAccumulatedValues`) will
226+
first gather the collection into an `Accumulator` and then return a stream over
227+
that accumulator. If not running in parallel, `from` is preferable (faster and
228+
less memory usage).
229+
230+
Note that a Scala `Iterator` cannot fulfill the contract of a Java 8 Stream
231+
(because it cannot support `trySplit` if it is called). Presently, one must
232+
call `fromAccumulated` on the `Iterator` to cache it, even if the Stream will
233+
be evaluated sequentially, or wrap it as a Java Iterator and use static
234+
methods in `Spliterator` to wrap that as a `Spliterator` and then a `Stream`.
235+
236+
Here is an example of conversion of a Scala collection within Java 8:
237+
219238
```java
220-
// TODO -- write converter and create example
239+
import scala.collection.mutable.ArrayBuffer;
240+
import scala.compat.java8.ScalaStreaming;
241+
242+
public class StreamConvertersExample {
243+
public int MakeAndUseArrayBuffer() {
244+
ArrayBuffer<String> ab = new ArrayBuffer<String>();
245+
ab.$plus$eq("salmon");
246+
ab.$plus$eq("herring");
247+
return ScalaStreaming.from(ab).mapToInt(x -> x.length()).sum(); // 6+7 = 13
248+
}
249+
}
221250
```

0 commit comments

Comments
 (0)