Scala Streams

Scala is to facilitate lazy operations. Since all calculations on these collections are deferred, they are not strictly defined. We will explore the Scala streams which are the unique types of lists in this article on streams in Scala. The Scala collection which stores the data also includes the Scala Stream. The only one difference that separates this from a list in Scala is when it is necessary for the Scala when the stream values are calculated. Because they don’t load the data all at once, Scala Streams are lazy lists that only evaluate the values as needed. This improves the program performance.

Syntax of the Stream in Scala in Ubuntu 20.04

The main difference between a Scala stream and a list is that a stream’s elements are computed slowly rather than all at once. Our application runs faster since only the elements that we request are computed. Scala’s stream, however, is speedy.

val stream = 1 #:: 2 #:: 3 #:: Stream.empty

A Stream may be created using the #:: operator approach and Stream.empty at the end of the expression as opposed to a List in Scala which can be created with the :: operator and Stream.empty. A stream of numbers is given as the specification of the stream. In this case, 1 represents the head of the stream. While 2 and 3 represent the tail of the stream. The take commands such as the ones in the following can be used to retrieve the values:


Example 1: Creating a Stream in Scala

The examples for creating a stream in Scala are given in the following illustrations. The elements in a stream are kept after being assessed.

We constructed the object as “Stream1”. It contains the main method definition inside the block of the main method. We made the Scala stream code. For this, we first declared the “val” keyword variable as “myStream”. The variable “myStream” is set with a stream of three numbers. We used 2, 4, and 10 as integers for the stream. Here, 2 is referred to as the head and the next two integers, 4 and 10, are referred to as the tail of the stream. We utilized the hashtag “#” and scope resolution “::” operator in between the integers. The stream.empty is employed at the end of the stream. Then, we called the “myStream” variable inside the println function to print the specified stream.

The outcomes show that the second part was not assessed. In this case, the element is replaced with a question mark. The lists are not evaluated by Scala until they are required. The tail is not printed as it has not yet been computed. The streams are designated for lazy computation.

Example 2: Creating a Stream with the Stream.Cons Package in Scala

Stream.cons can also be used to build a Stream. To generate streams, use the package import “scala.collection.immutable.Stream.cons”.

We mentioned the Scala package “scala.collection.immutable.Streams.cons” in our header section. After that, we made an object as “stream2”. The main method definition is provided for this object. We created the variable “s” with the help of the “val” keyword. We assigned the Stream[Int] to the variable “s”. Then, we called the cons of the stream. Each cons of the stream are enclosed inside the other. In the end, we used the stream.empty in the last cons. Then, we have the println method to print the Stream.cons elements. We utilized the string interpolation “${s}” to print the elements of the stream.

The immutable stream is generated in the following output screen. Only the head elements of the stream are obtained and the tail is not evaluated:

Example 3: Using the Take Function on Stream in Scala

To extract the elements from a stream, use the take function.

We established the object “stream3” and designated the main method for it. The main method contains the variable declaration and the print statement to display. The variable is declared here as “st” and initialized with the stream of three odd numbers.  The stream is created with the operator “#::” and the stream.empty at the end of the stream.

After the creation of the stream, we displayed it with the print function. Then, we took the first two elements from the given stream by passing the value “2” in the st.take() method. Next, we obtained the first five elements from the stream by passing the “5” in the stream take() method.

On the first evaluation of the stream, only one element is obtained. After using the stream take command, we retrieved the first two elements from the stream. Moreover, we fetched the first five elements from the stream. As only three elements are provided to the stream, so only three are accessed in the last output. Notice that no error is thrown.

Example 4: Using the Map on Stream in Scala

We use the map function on a stream in a Scala program. The stream operations are carried out using the map function.

We have an object “stream4” where the program’s main definition is called. Within the object “stream4” main function, we defined a “stream” variable. Here, we created the stream with the “#::” operator. The stream.empty is used at the end of the stream expression. The created stream is printed by passing the “stream” variable to the println function. Next, we called the map function inside another println function. We transformed the input stream with the new stream with the help of the map function.

The first obtained output is the steam and the next one is the new map stream from the previous stream.

Example 5: Initializing an Empty Stream in Scala

An empty Stream can be initialized using the following code:

Within the object “stream5”, we classified the main method. In the main method, we created the variable “S_empty” and set the empty stream. Then, we displayed the empty stream by utilizing the println method and passed the “S_empty” variable to it.

The output shows that the stream is empty. Moreover, it displays the empty stream but does not throw any kind of exception.


This is a Scala Stream reference manual. Here, we have gone through how to create the streams function in Scala using the appropriate programming examples. We did not compute the value of an element all at once in Scala Stream but rather one at a time. When performance is a problem, use this. When it comes to acquiring a stream element, it uses the most recent method. Since only one entry is computed at a time, Stream uses less memory than the List.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.