Scala Stream

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.

Operations on Stream

Create a Stream: Below is the examples to create a streams in Scala.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to creating an empty stream
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        // Creating stream
        val stream = 1 #:: 2#:: 8 #:: Stream.empty 
        println(stream) 
    
}

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to creating an stream
// using cons
import scala.collection.immutable.Stream.cons
  
// Creating object
object 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}")
    
}

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to Using take function on stream
  
// Creating object
object 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
    
}

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to using map function on stream
  
// Creating object
object 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}) 
    
}

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to create empty stream
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        // Creating empty stream
        val emptyStream: Stream[Int] = Stream.empty[Int]
        println(s"Empty Stream = $emptyStream")
    
}

chevron_right


Output:

 Empty Stream = Stream()


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.