@@ -143,7 +143,7 @@ obtain Java 8 Streams from Scala collections from within Java.
143
143
144
144
For sequential operations, Scala's ` iterator ` almost always equals or exceeds the performance of a Java 8 stream. Thus,
145
145
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 ` ,
147
147
` IntStream ` , and ` LongStream ` .
148
148
149
149
Note that although ` iterator ` typically has superior performance in a sequential context, the advantage is modest
@@ -173,13 +173,14 @@ object Test {
173
173
val m = collection.immutable.HashMap (" fish" -> 2 , " bird" -> 4 )
174
174
val s = m.parValueStream.sum // 6, potientially computed in parallel
175
175
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)]
177
177
178
- val n = a.splitter .fold(0 )(_ + _._1.length) +
178
+ val n = a.stepper .fold(0 )(_ + _._1.length) +
179
179
a.parStream.count // 8 + 2 = 10
180
180
181
181
val b = java.util.Arrays .stream(Array (2L , 3L , 4L )).
182
182
accumulate // LongAccumulator
183
+ val l = b.to[List ] // List(2L, 3L, 4L)
183
184
}
184
185
```
185
186
@@ -216,6 +217,34 @@ def mapToSortedString[A](xs: Vector[A], f: A => String, sep: String) =
216
217
217
218
#### Java Usage Example
218
219
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
+
219
238
``` 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
+ }
221
250
```
0 commit comments