Enumerator vs Iterator in scala and java

混江龙づ霸主 提交于 2019-12-22 08:33:48

问题


What is the difference between Enumerator and Iterator? Per my understanding Enumerator is not a fancy alias for enum in Java. Rather, it seems to be a traversal technique similar to an Iterator. So is anyone able to compare and contrast Enumerator and Iterator? Also, I see a usage in Play as

val data = getDataStream
val dataContent: Enumerator[Array[Byte]] = Enumerator.fromStream(data)

EDIT:

I am inclined to think that Enumerator gives us actual chunks of data, whereas Iterator gives us pointers to data that is already chunked, such as a list. But I am not convinced that that's the case.


回答1:


Enumerator is a Play class, not a Java or Scala one.

It is part of the Iteratee I/O-handling which Play provides. Iteratees are an interesting beast -- on one hand, it "pushes" data to the handler, instead of relying to the handler to pull data, and, therefore, has better performance. On the other hand, it allows the handler to control when the flow should be stopped. Finally, one can compose iteratees so that different iteratees are responsible for different parts of the input (say, one to receive HTTP headers, other to receive its body) as well as chaining them so low level iteratees can do a pre-processing which is then fed to higher level iteratees.

There's plenty of material on them, avail yourself to it.

It is not related in any way to iterators or enumerations.



来源:https://stackoverflow.com/questions/15032475/enumerator-vs-iterator-in-scala-and-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!