declare

How to create Maps in Scala (Scala Map class examples)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.13, “How to Create Maps in Scala”

Problem

You want to use a mutable or immutable Map in a Scala application.

Solution

To use an immutable map, you don’t need an import statement, just create a Map:

Different ways to create and update an Array in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.7, “Different ways to create and update an Array in Scala”

Problem

You want to create and optionally populate an Array.

Solution

There are many different ways to define and populate an Array. You can create an array with initial values, in which case Scala can determine the array type implicitly:

Different ways to create and populate Lists in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.1, “Different Ways to Create and Populate a List in Scala”

Problem

You want to create and populate a List.

Solution

There are many ways to create and initially populate a List:

Scala - How to declare multiple variables on one line

I thought I’d have a little fun with Scala today, and show different ways to declare multiple variables on one line. These aren’t the most useful or common things to do, but they’re interesting, and I know I learned at least one thing while looking into this.

You can define multiple fields on one line, separated by commas, as long as they are all the same type and have the same value: