Scala Collections: Lists, Sets, and Maps
Scala Lists
Lists are immutable sequences of elements of the same type.
var myList: List[String] = Nil var c: List[String] = Nil
Adding Elements to a List
c = "E" :: c
(At front)c = c :+ "MEOW"
(At end)
Removing Elements from a List
list diff List(num)
List Operations
List.range(0, 10, 2)
: Creates a list of numbers from 0 to 10 (exclusive), with a step of 2.var x = List.fill(3)("foo")
: Creates a list with three elements, all initialized to “foo”.var x = List.tabulate(5)(n => n * n)
: Creates a list using a function; in this case, a list of squares.
Concatenation
Use the ++
operator or :::
(for lists only):
list1 ++ list2 //> List(1, 2, 3, a, 2, true) list1 ::: list2 //> List(1, 2, 3, a, 2, true)
x.foreach(sum += _)
Sorting Lists
val b = List("banana", "pear", "apple", "orange").sorted // b: List[String] = List(apple, banana, orange, pear) List(10, 5, 8, 1, 7).sortWith(_ < _) // res1: List[Int] = List(1, 5, 7, 8, 10)
Mutable Lists (ArrayBuffer)
val mlist = mutable.ArrayBuffer("a", "b", "c")
Adding Elements (Mutable Lists Only)
mlist += "e" //> ArrayBuffer(a, b, c, e) mlist ++= List("f", "g") //> ArrayBuffer(a, b, c, e, f, g)
Modifying Items (Mutable Lists Only)
mlist(0) = "d" mlist //> ArrayBuffer(d, b, c, e, f, g)
Scala Sets
Sets are collections that contain no duplicate elements.
val b = scala.collection.mutable.Map[Int,Int]() scala.collection.mutable.Set()
Creating Sets
var s : Set[Int] = Set() // Empty set of integer type var s : Set[Int] = Set(1,3,5,7) // Set of integer type
Set Operations
- Concatenation:
S ++ S
- Add Element:
S + E
- Remove Element:
S - E
Set Tests
xs contains x
: Tests whetherx
is an element ofxs
.xs(x)
: Same asxs contains x
.xs subsetOf ys
: Tests whetherxs
is a subset ofys
.
Set Additions
xs + x
: The set containing all elements ofxs
as well asx
.xs + (x, y, z)
: The set containing all elements ofxs
as well as the given additional elements.xs ++ ys
: The set containing all elements ofxs
as well as all elements ofys
.
Set Removals
xs - x
: The set containing all elements ofxs
exceptx
.xs - (x, y, z)
: The set containing all elements ofxs
except the given elements.xs -- ys
: The set containing all elements ofxs
except the elements ofys
.xs.empty
: An empty set of the same class asxs
.
Input Methods
var a = scala.io.StdIn.readLine() io.Source.stdin.getLines().take(2). while(io.Source.stdin.hasNext) { println(io.Source.stdin.next().toString()) }
Scala Maps
Maps are collections of key-value pairs.
// Immutable var A:Map[Char,Int] = Map() // Create an empty map var states = scala.collection.mutable.Map[String, String]() // Create a map with initial elements var states = scala.collection.mutable.Map("AL" -> "Alabama", "AK" -> "Alaska")
Map Operations
- Add elements:
states += ("AZ" -> "Arizona")
- Add multiple elements:
states += ("CO" -> "Colorado", "KY" -> "Kentucky")
- Remove elements:
states -= "KY"
- Remove multiple elements:
states -= ("AZ", "CO")
- Update elements:
states("AK") = "Alaska, The Big State"
Combining Maps
// Use two or more Maps with ++ as operator var colors = colors1 ++ colors2 println( "colors1 ++ colors2 : " + colors ) // Use two maps with ++ as method colors = colors1.++(colors2) println( "colors1.++(colors2)) : " + colors ) colors.keys.foreach{ i => print( "Key = " + i ) println(" Value = " + colors(i) )} }
val b = scala.collection.mutable.Map[Int,Int]() for(i <- 0 until a.length) { if(b.contains(a(i))){ b(a(i)) = b(a(i)) + 1 } else b += (a(i) -> 1) }