20 Other Collection Types

20.1 The scala.collection Packages

    20.1.1 scala.collection.immutable

    20.1.2 scala.collection.mutable

20.2 Sets

    20.2.1 Running Through Sets

    20.2.2 Mutable vs. Immutable

    20.2.3 Using a Set

20.3 Maps

    20.3.1 Looping Through a Map

    20.3.2 Using Maps

20.4 Buffers

20.5 Collections as Functions

20.6 Project Integration

    20.6.1 Commands

    20.6.2 Adding Drawables

Additional Online Exercises:

O1. JSON Parsing: Many different web APIs using the JSON (JavaScript Object Notation) format instead of XML. An example of this is Twitter. Unfortunately, Twitter has changed their API to require authentication so pulling data from Twitter goes beyond the scope of an exercise. The Scala API provides a mechanism for parsing JSON data using the parser libraries. We will return to these in chapter 30. For now they provide an interesting example of other collections that you can play with in the REPL or a little program. You should import "util.parsing.json._" then you can parse JSON strings using JSON.parseFull(str:String). Try it with the following JSON strings. Note that because they include double quotes you probably want to use raw strings (with """).

[1, 2, 3]

{ "a" : 5, "b" : "hi"}

{ "a" : [1, 2 , { "c" : "testing", "d" : "objects" }], "b" : 42}

For this exercise you should write code that goes into the return value to get out different parts of it. Using a match expression and patterns can be very helpful here. You might try to write a function that takes an argument that is a string for a name and returns the values associated with that name that are nested in the JSON.