Title: Mutable Maps
Slug: mutable_maps
Summary: Mutable Maps Using Scala.
Date: 2017-04-03 12:00
Category: Scala
Tags: Basics
Authors: Chris Albon
If you want to learn more, check out Scala Cookbook and Programming in Scala.
In [14]:
val army = collection.mutable.Map(
"Tank" -> "A-1 Abrams",
"Aircraft" -> "F35",
"Ship" -> "Nimitz Class"
)
In [15]:
// Add an element
army += ("APC" -> "Bradley IFC")
// Add an element (alternative)
army.put("Weapon", "M60")
Out[15]:
In [16]:
// Add two elements
army += ("Helicopter" -> "Apache", "Missile" -> "Sidewinder")
Out[16]:
In [17]:
// Remove an element
army -= "Ship"
// Remove an element (alternative)
army.remove("Tank")
Out[17]:
In [18]:
// Change the value of an element
army("Tank") = "Tiger Tank"
In [19]:
// Keep only the key, value pairs that meet the criteria
army.retain((k,v) => k == "Tank")
Out[19]: