foreach: A Collection Method

In this lesson, you will be given a quick introduction to the foreach method.

We'll cover the following


As this chapter deals with collections, it is important to know how to print the elements in a collection.

We cannot simply use println or print as they are used to print single values and collections are a collection of values. For this reason, Scala provides the foreach method. This method can be used by every instance of every class in the collection library.


The foreach method is called on a collection of any type and takes a single argument. The argument must be a function/method that will be applied to each individual element of the collection.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy