Scala Tutorial - Learn How To Use Foreach Function With Examples

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:58 pm

Overview

In this tutorial, we will learn how to use the foreach function with examples on collection data structures in Scala. The foreach function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The foreach method takes a function as parameter and applies it to every element in the collection. As an example, you can use foreach method to loop through all elements in a collection.

 

As per the Scala documentation, the definition of the foreach method is as follows:


def foreach(f: (A) ⇒ Unit): Unit

 

The foreach method is a member of the IterableLike trait.

Steps

1. How to initialize a Sequence of donuts

The code below shows how to initialize a sequence of donut elements where each element in the sequence is of type String.


println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

 

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

2. How to loop through all the elements in the sequence using the foreach function

The code below shows how to loop through all elements in the donut sequence using the foreach method.


println("\nStep 2: How to loop through all the elements in the sequence using the foreach function")
donuts.foreach(println(_))

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to loop through all the elements in the sequence using the foreach function
Plain Donut
Strawberry Donut
Glazed Donut

NOTE:

  • Note that we are making use of the wildcard operator _ in the println method.

3. How to loop through and access all the elements in the sequence using the foreach function

The code below shows how to loop through and access all elements in the sequence using the foreach method.


println("\nStep 3: How to loop through and access all the elements in the sequence using the foreach function")
donuts.foreach(donutName => println(s"donutName = $donutName"))

You should see the following output when you run your Scala application in IntelliJ:

Step 3: How to loop through and access all the elements in the sequence using the foreach function
donutName = Plain Donut
donutName = Strawberry Donut
donutName = Glazed Donut

 

4. How to declare a value function to format a donut names into upper case format

The code below shows how to declare a Value Function which will format each donut name into uppercase.


println("\nStep 4: How to declare a value function to format a donut names into upper case format")
val uppercase: (String) => String = (s) => {
 val upper = s.toUpperCase
 println(upper)
 upper
}
println(s"Value function formatting donut names to uppercase = $uppercase")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to declare a value function to format a donut names into upper case format
Value function formatting donut names to uppercase = <function1>

 

5. How to format all donuts to uppercase using value function from Step 4

The code below shows how to use the Value Function from Step 4 and pass it through the foreach method in order to format each donut name into uppercase.


println("\nStep 5: How to format all donuts to uppercase using value function from Step 4")
donuts.foreach(uppercase)

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to format all donuts to uppercase using value function from Step 4
PLAIN DONUT
STRAWBERRY DONUT
GLAZED DONUT

 

This concludes our tutorial on Learn How To Use Foreach Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize a Sequence of donuts
  • How to loop through all the elements in the sequence using the foreach function
  • How to loop through and access all the elements in the sequence using the foreach function
  • How to declare a value function to format a donut names into upper case format
  • How to format all donuts to uppercase using value function from Step 4

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use groupBy function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on