Updated April 15, 2023
Introduction to Scala Stream
Scala Stream is also a part of scala collection which store data. This is similar to list in scala only with one difference. In scala stream value will only be calculated when needed Scala Stream are lazy list which evaluates the values only when it is required, hence increases the performance of the program by not loading the value at once.
Syntax:
valstream_name = value1 #:: value2 #:: value3 #:: Stream.empty
In scala we use #:: operator to create a stream and assign them value. But in list we only use :: operator to create them. Also, in scala stream we use Stream.empty at the end of the syntax.
valmyStream = 10 #:: 20 #:: 30 #:: Stream.empty
So in this way we can assign value to s stream, but it will only calculate the first value.
How Stream works in Scala?
As we have seen that scala stream is lazy list, they are like list on scala with only difference is that their element are computed lazily not at once. Only the element’s which we request are computed thus increases the performance of our program. But stream in scala is fast.
Example:
varmystm = 10 #:: 20 #:: 30 #:: Stream.empty
In this the head of the stream is 10 and 20, 30 are the tails of the stream. So in output also the head will be printed but not the tail because they have not been computed yet. In this we have 3 elements in the stream.
In scala we have Sequence, List and Stream.
1. Sequence
This is also a part of collection which stores elements or we can say sequence of elements. But this collection stores the elements in some specific order. But we cannot change order of the element once we defined them into the collection. This leads to sequential processing of their element so can take more time to process them.
Package :scala.collection.Seq>> In this package we can find this class.
2. List
This is also a part of collection in scala. This is also used to store group of elements and this is the sub class of sequence in scala. List in scala are the default implementation of sequence. List process its all elements one by one. It is not strict to compute only one value.
Package :scala.collection.immutable.List>> In this we can find this class.
3. Stream
This is also part of collection except one change it is based on lazy approach. That means only one element will be computed at a time. Otherwise it has all the characteristics like list in scala. Stream is also a sub class for sequence.
Both List and Stream process their elements parallel so can improve performance and if you want any of your collection to process parallel then you will need >>scala.collection.parallel. Stream requires less memory than List because it computed only one element at a time.
Performance Characteristics: As performance point of view we cannot decide which can be better but as we know stream is based on the lazy approach which computed elements when it is required so we can say it can provide us better performance compared to other collections.
Immutable | Head | Tail | Apply | Update | Prepend | Append | Insert |
Stream | C | C | L | L | C | L | – |
List | C | C | L | L | C | L | – |
We can have a look at it performance characteristics of stream and list collection in scala. In case of immutable.
Here constants mean:
- L: This symbol defines that this operation depends upon the size of collection, we can say it is proportional to length of collection it is liner in nature.
- C: This will take constant time.
- -: This symbol means this operation is not supported in the mentioned collection.
Examples of Scala Stream
Given below are the examples mentioned:
Example #1
In this example we are creating different stream list.
Code:
object Main extends App{
// Initializing the stream with values.
val mystm1 = 001 #:: 002 #:: 003 #:: Stream.empty
val mystm2 = "abc" #:: "aa" #:: "bb" #:: Stream.empty
val mystm3 = 150 #:: 250 #:: 350 #:: Stream.empty
val mystm4 = 401 #:: 509 #:: 687 #:: Stream.empty
//print stream
println("Values in stream first ::" + mystm1)
println("Values in stream second ::" + mystm2)
println("Values in stream third ::" + mystm3)
println("Values in stream four ::" + mystm4)
}
Output:
Example #2
In this example we are retrieving head of each stream list.
Code:
object Main extends App{
// Initializing the stream with values.
val mystm1 = 001 #:: 002 #:: 003 #:: Stream.empty
val mystm2 = "abc" #:: "aa" #:: "bb" #:: Stream.empty
val mystm3 = 150 #:: 250 #:: 350 #:: Stream.empty
val mystm4 = 401 #:: 509 #:: 687 #:: Stream.empty
//print sstream head
println("Head of first stream ::" + mystm1.head)
println("Head of second stream ::" + mystm2.head)
println("Head of third stream ::" + mystm3.head)
println("Head of fourth stream ::" + mystm4.head)
}
Output:
Example #3
In this we are retrieving tail of the stream.
Code:
object Main extends App{
// Initializing the stream with values.
val mystm1 = 001 #:: 002 #:: 003 #:: Stream.empty
//print sstream tail
println("Head of first stream ::" + mystm1.tail)
}
Output:
Example #4
Print all value of stream.
Code:
object Main extends App{
val mystm1 = 001 #:: 002 #:: 003 #:: Stream.empty
val mystm2 = "abc" #:: "aa" #:: "bb" #:: Stream.empty
val mystm3 = 150 #:: 250 #:: 350 #:: Stream.empty
val mystm4 = 401 #:: 509 #:: 687 #:: Stream.empty
mystm1.toStream.foreach(x =>
println("all elements of the stream ::" + x)
)
mystm2.toStream.foreach(x =>
println("all elements of the stream ::" +x)
)
mystm3.toStream.foreach(x =>
println("all elements of the stream ::" + x)
)
mystm4.toStream.foreach(x =>
println("all elements of the stream ::" + x)
)
}
Output:
Conclusion
In Scala Stream we computed the value of element one at a time not all at once. This can be used where performance is a concern. It follows the last approach while it comes to accessing the element of a stream.
Recommended Articles
We hope that this EDUCBA information on “Scala Stream” was beneficial to you. You can view EDUCBA’s recommended articles for more information.