Stream memoises and Iterator does not. You can traverse the same Stream multiple times and get the same result each time. Iterator, on the other hand, can only be traversed once.
They are both constructs for accessing a current element, having a yet unknown list of remaining elements (the lazy tail).
Iterator is an imperative construct which you can only traverse once.
Stream is a functional construct. In theory you can traverse it multiple times (and as others mentioned, it won't recompute the already computed parts), but in practice because Streams are either infinite or very large (that is why you use it in the first place), holding reference to the full stream doesn't make much sense (you run into Out Of Memory pretty easy).
- Therefore you should always define streams using
defand never put it into local variables which have a long-lived scope.
- There are also subtleties when writing recursive functions using Streams,
- There can be some unexpected behavior resulting from the fact that Scala's
Streamis not lazy in its head, like
Generally it is safer to the mind to avoid plain
Streams. Alternatives are using
EphemeralStream of Scalaz which auto-forgets unreferred parts using weak references, or using Iteratees (see also here) or something similiar.
- Difference between Iterator and Stream in Scala?
- Difference between object and class in Scala
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- Difference between a Seq and a List in Scala
- Difference between method and function in Scala
- difference between foldLeft and reduceLeft in Scala
- Difference between Array and List in scala
- What’s the difference between ScalaTest and Scala Specs unit test frameworks?
- Scala: What is the difference between Traversable and Iterable traits in Scala collections?
- Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)?
- Difference between using App trait and main method in scala
- What's the difference between :: and ::: in Scala
- Scala - What is the difference between size and length of a Seq?
- What is the difference between a class and a type in Scala (and Java)?
- What's the difference between raw string interpolation and triple quotes in scala
- In Scala Akka futures, what is the difference between map and flatMap?
- The difference between NonFatal and Exception in Scala
- Difference between Scala Option's isDefined and nonEmpty method
- What is the difference between the methods iterator and view?
- Difference between filter and where in scala spark sql
- What is the difference between scala StrictLogging and Lazylogging?
- What is the difference between scala classes, scripts and worksheets in Intellij-idea?
- Difference between Java Optional and Scala Option
- How does Scala know the difference between "def foo" and "def foo()"?
- Difference between F[_] and F[T] In Scala when used in type constructors
- Difference between >> and >>> in Scala
- Scala macros: What is the difference between typed (aka typechecked) and untyped Trees
- What is the difference between Expressions and Statements in Scala
- What is the difference between Array and WrappedArray in Scala
- What's the difference between Erlang Actors, Scala Actors and the theoretical concept "Actor"?
More Query from same tag
- What is the difference between Abstract Data Types and Algebraic Data Types
- Spark Standalone Cluster deployMode = "cluster": Where is my Driver?
- How does the following Java "continue" code translate to Scala?
- How to Conditionally Produce JSON Using JSON4S
- How to use ~> and <~ in grammar rule definition in Scala?
- save jdbc response and iterate to next request
- Scala: duplicate execution of method in Map values
- How can I set up Scala-IDE to add a blank line when I start a multiline code block?
- Typeclass with constructor functions
- Stream paginated API response over WebSocket using Akka
- Spark + MySQL: no spark.read
- Can Cassandra nest Sets of UDTs?
- Regex - All valid identifiers except for a given word?
- Can I use Lift's FieldSerializer to change a field on serialization?
- Avoid repeating package names in Scala.js calls from HTML
- Use external libraries inside the build.sbt file
- scala concatenate string inside a list conditionally
- Why is there an implicit conversion from Float/Double to BigDecimal, but not from String?
- Making HTTP post requests on Spark usign foreachPartition
- Confused about seemingly superfluous respecification of a variable's type in a genericized scala function
- Call method only once and apply filter to save result in different variables in scala
- libraries for external DSL evaluation in Scala
- What is the cause of this strange Scala memory leak?
- Spark Structured Streaming recovering from a query exception
- Connecting cassandra cluster through scala
- Comparisons between generic types in Scala
- synchronized write to a file
- Compare size of 2 Arrays in Scala
- Scala: Why does zipped method succeed with tuples of List and not Traversable?
- "object is not a member of package" when importing dependency built from another project via slick codegen