collections

How to create a mutable Set in Scala

Scala Set FAQ: How do I create a mutable Set in Scala?

To create a mutable set in Scala, first determine the type of set you want. You do this because the mutable Set is actually a trait, and you need to determine which concrete implementation you want.

For instance, if you want a SortedSet of String, define it like this:

val names = scala.collection.mutable.SortedSet[String]()

Then you can add elements to the set later like this:

Wanted: Scala Cookbook reviewers

UPDATE: I originally posted this article in January, 2013, and it's now mid-February, 2013, and we're no longer looking for reviewers. I've only kept this page here so people won't get 404 errors.

OLD CONTENT:

Interested in being a reviewer for the Scala Cookbook?

A collection of Scala flatMap examples

Scala flatMap FAQ: Can you share some Scala flatMap examples?

Sure. When I was first trying to learn Scala, and cram the collections' flatMap method into my brain, I scoured books and the internet for great flatMap examples. Once I had a little grasp of it I started creating my own examples, and tried to keep them simple.

Using flatMap on a list of String

The following examples show the differences between map and flatMap on a sequence of String:

Scala reduceLeft examples

The reduceLeft method on the Scala collections is fun. Just start with a collection:

scala> val a = Array(20, 12, 6, 15, 2, 9)
a: Array[Int] = Array(20, 12, 6, 15, 2, 9)

Then give reduceLeft a simple function to work with, and let it do its thing:

Getting the union, intersection, and difference of Scala sets

A cool thing about Scala sets -- and some other Scala collections -- is that you can easily determine the union, intersection, and difference between two sets. The following examples demonstrate how the methods work. First, we create two sets that have a slight overlap:

scala> val low = 1 to 5 toSet
low: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> val medium = (3 to 7).toSet
medium: scala.collection.immutable.Set[Int] = Set(5, 6, 7, 3, 4)

Now we exercise the methods. First, the union:

Scala - Merging two Arrays or ArrayBuffers

Scala Array FAQ: How do I merge two Arrays or ArrayBuffers?

Solution: Use the ++ method to join two arrays into one new array:

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> val c = a ++ b
c: Array[Int] = Array(1, 2, 3, 4, 5, 6)

ArrayBuffer

Use the same approach to merge two ArrayBuffers into a new ArrayBuffer:

Scala collections, and life in a parallel universe (a quick look)

If you ever want to have a little fun with Scala, try experimenting with the Scala parallel collections. The following example gives you a little idea of what can happen in the most simple case of creating and using a parallel collection:

Scala parallel programming, parallel collections, .par, and performance

To be clear, these examples of using Scala parallel collections aren't my own examples, they come from this page on the scala-lang.org website. But, for the completeness of my Scala cookbook recipes, I wanted to make sure I included a reference to parallel collections here.

Scala cookbook recipes

Wow, I began by writing a few Scala programming tutorials just because I like the language, and as I look here a couple of months later I now have more than sixty tutorials. As a result, I thought I'd start organizing them here in the form a Scala Programming Cookbook.

Here's my current collection of Scala FAQs and recipes, in a "cookbook" format.

Converting Java collections to Scala collections

While working on a new Scala application, I just ran into a situation where a Java class I was using returned a Java List to me. I wasn't exactly thinking too hard at that moment, and didn't realize it was actually a Java List until I tried to use the foreach method on it, and Eclipse balked at me.

Syndicate content