Introduction
Syntax
Introduction
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.
Syntax
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.
For printing the elements of a collection, we would need to apply the println or print method to each element.