简体   繁体   English

Scala相当于java.util.stream

[英]Scala equivalent of java.util.stream

Is there an Scala type (probably a trait) similar to java.util.stream.Stream ? 是否存在类似于java.util.stream.Stream的Scala类型(可能是特征)?

(I am using the better-supported Java 7, so Java 8's Stream is not available to me.) (我使用的是支持更好的Java 7,因此我无法使用Java 8的Stream 。)

Specifically, it must be generically typed, be iterable in some way, and be closeable. 具体来说,它必须是通用类型,可以某种方式迭代,并且可以关闭。

try {
  stream.foreach(println)
  }
} finally {
  stream.close()
}

A solution in the standard libraries would be preferred, though something in another library would be acceptable. 标准库中的解决方案将是首选,但另一个库中的某些内容是可以接受的。

For collections, all collections in Scala can be used as if they are streams. 对于集合,Scala中的所有集合都可以像流一样使用。 In Scala the operators on Iterable / Iterator are lazily applied for example and you also have Views and Stream ( Stream is a lazily evaluated List and does memoization of the traversed values, so if you want memoization you can use a stream, but because of the memory requirements this it tricky for working with infinite streams - still it's pretty cool). 在Scala中, Iterable / Iterator上的运算符是懒惰地应用的例子,你也有视图Stream是一个懒惰评估的List并且对遍历值进行记忆,所以如果你想要memoization你可以使用一个流,但是因为内存要求这对于使用无限流来说很棘手 - 但它仍然非常酷。

As you might have noticed however, this doesn't take care of all cases, because the stream could be a stream of data coming from a network socket, file handle and so on. 然而,您可能已经注意到,这并没有考虑所有情况,因为流可能是来自网络套接字,文件句柄等的数据流。 So one needs to dispose of the underlying resources used. 因此,需要处理所使用的底层资源。

I do not know of a direct equivalent, however I must also mention that I do not like Java's streams, because Java's streams are for processing iterable sequences, which means that their current design is pull-based. 我不知道直接的等价物,但是我还必须提到我不喜欢Java的流,因为Java的流用于处理可迭代序列,这意味着它们当前的设计是基于拉取的。 This may change in the future, but when processing streams, especially streams involving I/O, it's better to go non-blocking and asynchronous, therefore I prefer push based approaches, like Iteratees , or the Reactive Extensions (Rx) pattern. 这可能在将来发生变化,但在处理流时,尤其是涉及I / O的流时,最好采用非阻塞和异步,因此我更喜欢基于推送的方法,如Iteratees或Reactive Extensions(Rx)模式。

A lot is going on in the Scala/Java ecosystem in this regard. 在这方面,Scala / Java生态系统正在发生很多事情。 There is RxJava , which is an Rx.NET port. RxJava ,它是一个Rx.NET端口。 There's also Akka Streams , part of the Reactive Streams effort to specify an interoperable protocol for back-pressure - in the experimental/alpha stage. 还有Akka Streams ,它是Reactive Streams努力的一部分,用于指定背压的可互操作协议 - 在实验/ alpha阶段。

And a shameless promotion - I'm working on Monifu , an Rx implementation for Scala, that does back-pressure and that will also be integrated with the "Reactive Streams" protocol. 这是一个无耻的推广 - 我正在开发Monifu ,一个用于Scala的Rx实现,它可以实现反压,并且还可以与“Reactive Streams”协议集成。 And I also think it's already in a better shape and more idiomatic than the RxScala adaptor of RxJava, but since I'm the author, take that with a grain of salt :-) 而且我也认为它已经比RxJava的RxScala适配器更好的形状和更惯用,但是因为我是作者,所以带上一粒盐:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM