Scala Stream - GeeksforGeeks

07 Aug, 2019
The Stream is a lazy lists where elements are evaluated only when they are needed. This is a scala feature. Scala supports lazy computation. It increases performance of our program. Streams have the same performance characteristics as lists.
Syntax :
val str = 1 #:: 2 #:: 3 #:: Stream.empty
In scala a List can be constructed with :: operator, whereas a Stream can be constructed with the #:: operator method, using Stream.empty at the end of the expression. In above syntax the head of this stream is 1, and the tail of it has 2 and 3.
Create a Stream: Below is the examples to create a streams in Scala.Example :
// Program to creating an empty stream  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         // Creating stream        val stream = 1 #:: 2#:: 8 #:: Stream.empty         println(stream)     } }
Output:
Stream(1, ?)
In the above output, we can see that second element is not evaluated. Here, a question mark is displayed in place of element. Scala does not evaluate list until it is required. The tail is not printed, because it hasn’t been computed yet. Streams are specified to lazy computation. Create a Stream using Stream.cons : We can also create a Stream by using Stream.cons . A package import scala.collection.immutable.Stream.cons is used for creating stream.
Example :
// Program to creating an stream// using consimport scala.collection.immutable.Stream.cons  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         // Creating stream        val stream2: Stream[Int] = cons(1, cons(2, cons(3, Stream.empty) ) )        println(s"Elements of stream2 = ${stream2}")    } }
Output:
Elements of stream2 = Stream(1, ?)
 Using take function on stream: take function is used to take elements from stream. Below is the example of using take function.
Example :
// Program to Using take function on stream  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         // Creating stream        val stream = 1 #:: 2#:: 8 #:: Stream.empty         println(stream)                   // Taking elements from stream        print("Take first 2 numbers from stream = ")        stream.take(2).print        print("\nTake first 10 numbers from stream2 = ")        stream.take(10).print    } }
Output :
Stream(1, ?)
Take first 2 numbers from stream = 1, 2, empty
Take first 10 numbers from stream2 = 1, 2, 8, empty
When we wanted to take 10 numbers from a Stream, although it only contained 3 elements, it did not throw any IndexOutOfBoundsException. 
Using map function on stream: map function is used to perform operation on stream.
Example :
// Scala program to using map function on stream  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         // Creating stream        val stream = 1 #:: 2#:: 8 #:: Stream.empty         println(stream)                   // map elements from stream        println(stream.map{_+5})     } }
Output:
Stream(1, ?)
Stream(6, ?)
In above example by using map function we are transforming the input collection to a new output collection. Initialize an empty Stream: Below code shows how to initialize an empty Stream.Example :
// Program to create empty stream  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         // Creating empty stream        val emptyStream: Stream[Int] = Stream.empty[Int]        println(s"Empty Stream = $emptyStream")    } }
Output:
 Empty Stream = Stream()
scala-collection
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Throw Keyword in Scala
Scala | Functions Call-by-Name
Comments In Scala
How to install Scala on Windows?
HashMap in Scala
Operators in Scala
Scala Singleton and Companion Objects
Type Casting in Scala
Scala map contains() method with example