Code Examples for

Programming in Scala, Second Edition

Return to chapter index

17 Collections

Sample run of chapter's interpreter examples

17.1 Sequences


scala> val colors = List("red", "blue", "green") colors: List[java.lang.String] = List(red, blue, green) scala> colors.head res0: java.lang.String = red scala> colors.tail res1: List[java.lang.String] = List(blue, green)
scala> val fiveInts = new Array[Int](5) fiveInts: Array[Int] = Array(0, 0, 0, 0, 0)
scala> val fiveToOne = Array(5, 4, 3, 2, 1) fiveToOne: Array[Int] = Array(5, 4, 3, 2, 1)
scala> fiveInts(0) = fiveToOne(4) scala> fiveInts res3: Array[Int] = Array(1, 0, 0, 0, 0)
scala> import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer scala> val buf = new ListBuffer[Int] buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer() scala> buf += 1 res4: buf.type = ListBuffer(1) scala> buf += 2 res5: buf.type = ListBuffer(1, 2) scala> buf res6: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2) scala> 3 +=: buf res7: buf.type = ListBuffer(3, 1, 2) scala> buf.toList res8: List[Int] = List(3, 1, 2)
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
scala> val buf = new ArrayBuffer[Int]() buf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> buf += 12 res9: buf.type = ArrayBuffer(12) scala> buf += 15 res10: buf.type = ArrayBuffer(12, 15) scala> buf res11: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(12, 15)
scala> buf.length res12: Int = 2 scala> buf(0) res13: Int = 12
scala> def hasUpperCase(s: String) = s.exists(_.isUpper) hasUpperCase: (s: String)Boolean scala> hasUpperCase("Robert Frost") res14: Boolean = true scala> hasUpperCase("e e cummings") res15: Boolean = false

17.2 Sets and maps


object Predef { type Map[A, +B] = collection.immutable.Map[A, B] type Set[A] = collection.immutable.Set[A] val Map = collection.immutable.Map val Set = collection.immutable.Set // ... }
scala> import scala.collection.mutable import scala.collection.mutable
scala> val mutaSet = mutable.Set(1, 2, 3) mutaSet: scala.collection.mutable.Set[Int] = Set(3, 1, 2)
scala> val text = "See Spot run. Run, Spot. Run!" text: java.lang.String = See Spot run. Run, Spot. Run! scala> val wordsArray = text.split("[ !,.]+") wordsArray: Array[java.lang.String] = Array(See, Spot, run, Run, Spot, Run)
scala> val words = mutable.Set.empty[String] words: scala.collection.mutable.Set[String] = Set()
scala> for (word <- wordsArray) | words += word.toLowerCase scala> words res17: scala.collection.mutable.Set[String] = Set(spot, run, see)
scala> val map = mutable.Map.empty[String, Int] map: scala.collection.mutable.Map[String,Int] = Map()
scala> val map = mutable.Map.empty[String, Int] map: scala.collection.mutable.Map[String,Int] = Map()
scala> map("hello") = 1 scala> map("there") = 2 scala> map res20: scala.collection.mutable.Map[String,Int] = Map(hello -> 1, there -> 2)
scala> map("hello") res21: Int = 1
scala> def countWords(text: String) = { | val counts = mutable.Map.empty[String, Int] | for (rawWord <- text.split("[ ,!.]+")) { | val word = rawWord.toLowerCase | val oldCount = | if (counts.contains(word)) counts(word) | else 0 | counts += (word -> (oldCount + 1)) | } | counts | } countWords: (text: String)scala.collection.mutable.Map[String,Int] scala> countWords("See Spot run! Run, Spot. Run!") res22: scala.collection.mutable.Map[String,Int] = Map(see -> 1, run -> 3, spot -> 2)
scala> import scala.collection.immutable.TreeSet import scala.collection.immutable.TreeSet scala> val ts = TreeSet(9, 3, 1, 8, 0, 2, 7, 4, 6, 5) ts: scala.collection.immutable.TreeSet[Int] = TreeSet(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) scala> val cs = TreeSet('f', 'u', 'n') cs: scala.collection.immutable.TreeSet[Char] = TreeSet(f, n, u)
scala> import scala.collection.immutable.TreeMap import scala.collection.immutable.TreeMap scala> var tm = TreeMap(3 -> 'x', 1 -> 'x', 4 -> 'x') tm: scala.collection.immutable.TreeMap[Int,Char] = Map(1 -> x, 3 -> x, 4 -> x) scala> tm += (2 -> 'x') scala> tm res30: scala.collection.immutable.TreeMap[Int,Char] = Map(1 -> x, 2 -> x, 3 -> x, 4 -> x)

17.3 Selecting mutable versus immutable collections


scala> val people = Set("Nancy", "Jane") people: scala.collection.immutable.Set[java.lang.String] = Set(Nancy, Jane) scala> people += "Bob" <console>:11: error: reassignment to val people += "Bob" ^
scala> var people = Set("Nancy", "Jane") people: scala.collection.immutable.Set[java.lang.String] = Set(Nancy, Jane) scala> people += "Bob" scala> people res34: scala.collection.immutable.Set[java.lang.String] = Set(Nancy, Jane, Bob)
scala> people -= "Jane" scala> people ++= List("Tom", "Harry") scala> people res37: scala.collection.immutable.Set[java.lang.String] = Set(Nancy, Bob, Tom, Harry)
// In file collections/Capitals.scala var capital = Map("US" -> "Washington", "France" -> "Paris") capital += ("Japan" -> "Tokyo") println(capital("France"))
// In file collections/Capitals.scala import scala.collection.mutable.Map // only change needed! var capital = Map("US" -> "Washington", "France" -> "Paris") capital += ("Japan" -> "Tokyo") println(capital("France"))
scala> var roughlyPi = 3.0 roughlyPi: Double = 3.0 scala> roughlyPi += 0.1 scala> roughlyPi += 0.04 scala> roughlyPi res40: Double = 3.14

17.4 Initializing collections


scala> List(1, 2, 3) res41: List[Int] = List(1, 2, 3) scala> Set('a', 'b', 'c') res42: scala.collection.immutable.Set[Char] = Set(a, b, c) scala> import scala.collection.mutable import scala.collection.mutable scala> mutable.Map("hi" -> 2, "there" -> 5) res43: scala.collection.mutable.Map[java.lang.String,Int] = Map(hi -> 2, there -> 5) scala> Array(1.0, 2.0, 3.0) res44: Array[Double] = Array(1.0, 2.0, 3.0)
scala> import scala.collection.mutable import scala.collection.mutable scala> val stuff = mutable.Set(42) stuff: scala.collection.mutable.Set[Int] = Set(42) scala> stuff += "abracadabra" <console>:15: error: type mismatch; found : java.lang.String("abracadabra") required: Int stuff += "abracadabra" ^
scala> val stuff = mutable.Set[Any](42) stuff: scala.collection.mutable.Set[Any] = Set(42)
scala> val colors = List("blue", "yellow", "red", "green") colors: List[java.lang.String] = List(blue, yellow, red, green)
scala> import scala.collection.immutable.TreeSet import scala.collection.immutable.TreeSet scala> val treeSet = TreeSet(colors) <console>:15: error: could not find implicit value for parameter ord: Ordering[List[java.lang.String]] val treeSet = TreeSet(colors) ^
scala> val treeSet = TreeSet[String]() ++ colors treeSet: scala.collection.immutable.TreeSet[String] = TreeSet(blue, green, red, yellow)
scala> treeSet.toList res50: List[String] = List(blue, green, red, yellow)
scala> treeSet.toArray res51: Array[String] = Array(blue, green, red, yellow)
scala> import scala.collection.mutable import scala.collection.mutable scala> treeSet res52: scala.collection.immutable.TreeSet[String] = TreeSet(blue, green, red, yellow) scala> val mutaSet = mutable.Set.empty ++= treeSet mutaSet: scala.collection.mutable.Set[String] = Set(yellow, blue, red, green) scala> val immutaSet = Set.empty ++ mutaSet immutaSet: scala.collection.immutable.Set[String] = Set(yellow, blue, red, green)
scala> val muta = mutable.Map("i" -> 1, "ii" -> 2) muta: scala.collection.mutable.Map[java.lang.String,Int] = Map(ii -> 2, i -> 1) scala> val immu = Map.empty ++ muta immu: scala.collection.immutable.Map[java.lang.String,Int] = Map(ii -> 2, i -> 1)

17.5 Tuples


// In file collections/Misc.scala (1, "hello", Console)
def longestWord(words: Array[String]) = { var word = words(0) var idx = 0 for (i <- 1 until words.length) if (words(i).length > word.length) { word = words(i) idx = i } (word, idx) }
scala> val longest = | longestWord("The quick brown fox".split(" ")) longest: (String, Int) = (quick,1)
scala> longest._1 res53: String = quick scala> longest._2 res54: Int = 1
scala> val (word, idx) = longest word: String = quick idx: Int = 1 scala> word res55: String = quick
scala> val word, idx = longest word: (String, Int) = (quick,1) idx: (String, Int) = (quick,1)

17.6 Conclusion

For more information about Programming in Scala, Second Edition (the "Stairway Book"), please visit:

http://www.artima.com/shop/programming_in_scala_2ed

and:

http://booksites.artima.com/programming_in_scala_2ed

Copyright © 2007-2010 Artima, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.