

Empty hash table whose keys are strings and values are integers: The Following is the example statements to declare immutable Maps − If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable.Map. If you want to use the mutable Map, you'll have to import class explicitly. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed.īy default, Scala uses the immutable Map. There are two kinds of Maps, the immutable and the mutable. Keys are unique in the Map, but values need not be unique. Any value can be retrieved based on its key. The for comprehension is not a looping construct, but is a syntactic construct the compiler reduces to map, flatMap, and filter.Scala map is a collection of key/value pairs. Scala provides the for comprehension, which provides syntactically pleasing nesting of map, flatMap, and filter. I could easily write an entire tutorial on the Scala for comprehension, so to keep this tutorial short, I'll stop here for now.īefore leaving, I will add these notes however, from the book Programming in Scala: If you already know about the for comprehension, you know that you can add multiple if clauses, and much more functionality. Scala> for (name <- names if name.startsWith("J")) Scala> for (name val names = Vector("Bob", "Fred", "Joe", "Julia", "Kim") Names: Vector = Vector(Bob, Fred, Joe, Julia, Kim) Scala> val names = Vector("Bob", "Fred", "Joe", "Julia", "Kim")
#SCALA FOREACH HOW TO#
Here's a simple example of how to iterate over a sequence using the for comprehension (also known as a “ for loop”): The Scala for comprehension is not specific to lists, but is an extremely powerful way to operate on a List and other sequences. (When I first wrote this example it wasn’t the worst thing in the world to use a var field, but with more and more developers preferrring functional programming, the use of var fields is discouraged.) Scala Lists and the “for comprehension” Note that this second example is not a common or preferred way to use foreach I’m just trying to show some different possibilities. This next example shows a way to sum all the elements in a list using foreach: Because foreach takes a procedure that doesn’t return anything, and because the result of foreach is also Unit, the foreach method is typically used for its side effects - something like this example where output is printed for a user to see. Note that this is a relatively common way to use the foreach method. If you’ve used a programming language like Ruby, this syntax will look familiar to you. Here’s a simple example showing how to use foreach to print every item in a List:

The result of the operation is again Unit no list of results is assembled. It simply applies the procedure to each List element. Here's a quote about foreach from the book Programming in Scala:įoreach takes a procedure - a function with a result type Unit - as the right operand. Iterating over lists with ‘foreach’Ī common way to iterate over a Scala List is with the foreach method. There are a number of ways to iterate over a Scala List using the foreach method (which is available to Scala sequences like List, Array, ArrayBuffer, Vector, Seq, etc.) and for comprehension, and I'll show a few of those approaches here. Scala List/sequence FAQ: How do I iterate over a Scala List (or more generally, a sequence) using the foreach method or for loop? show more info on classes/objects in repl.
