Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, the Map
class provides various methods to work with key-value pairs. Here's a collection of some commonly used methods that you can call on a Map
:
apply
:
Returns the value associated with a given key.
val map = Map("a" -> 1, "b" -> 2) val valueA = map("a") // 1
get
:
Returns an Option
with the value associated with a given key.
val valueOption = map.get("c") // None, since "c" doesn't exist
getOrElse
:
Returns the value associated with a given key, or a default if the key is not present.
val valueB = map.getOrElse("b", 0) // 2 val valueC = map.getOrElse("c", 0) // 0
contains
:
Checks if the map contains a specific key.
val hasA = map.contains("a") // true
isEmpty
:
Checks if the map is empty.
val emptyCheck = map.isEmpty // false
+
:
Adds a key-value pair (or pairs) and returns a new map.
val newMap = map + ("c" -> 3)
-
:
Removes a key (or keys) and returns a new map.
val reducedMap = map - "a"
keys
:
Returns an iterable of all keys.
val keys = map.keys // Set(a, b)
values
:
Returns an iterable of all values.
val values = map.values // Iterable(1, 2)
foreach
:
Iterates over each key-value pair.
map.foreach { case (key, value) => println(s"$key: $value") }
map
:
Transforms the map. For example, squaring each value:
val squaredMap = map.map { case (key, value) => (key, value * value) }
++
:
Merges two maps. In case of duplicate keys, the values from the second map will overwrite the first.val anotherMap = Map("b" -> 10, "c" -> 11) val merged = map ++ anotherMap // Map(a -> 1, b -> 10, c -> 11)
filter
:
Filters the map based on a predicate.
val filtered = map.filter { case (_, value) => value > 1 } // Map(b -> 2)
filterKeys
:
Filters the map based on keys.
val filteredByKeys = map.filterKeys(_ == "a") // Map(a -> 1)
head
, last
:
Retrieve the first and last key-value pairs, respectively.
take
, drop
:
Take or drop the first 'n' elements.
This list is not exhaustive, but it covers many common operations you might want to perform on a Map
. Remember that most operations on an immutable Map
will return a new Map
with the desired modifications, leaving the original unchanged. If you're working with a mutable Map
, methods may modify the map in place.
Common Methods for Accessing Values in Scala Map:
Access values using keys in a Map.
val myMap = Map("one" -> 1, "two" -> 2, "three" -> 3) val valueOne = myMap("one") val valueOrDefault = myMap.getOrElse("four", 0)
Iterating Over Keys and Values in Scala Map:
Iterate over keys and values using foreach
.
val myMap = Map("one" -> 1, "two" -> 2, "three" -> 3) myMap.foreach { case (key, value) => println(s"Key: $key, Value: $value") }
Adding and Removing Elements in Scala Map:
Add and remove elements in a mutable Map.
var mutableMap = scala.collection.mutable.Map("one" -> 1, "two" -> 2) mutableMap += ("three" -> 3) mutableMap -= "two"
Transforming Values with map()
on Scala Map:
Transform values using map()
.
val myMap = Map("one" -> 1, "two" -> 2, "three" -> 3) val squaredMap = myMap.mapValues(value => value * value)
Filtering Map Entries in Scala:
Filter Map entries based on a condition.
val myMap = Map("one" -> 1, "two" -> 2, "three" -> 3) val filteredMap = myMap.filter { case (_, value) => value % 2 == 0 }
Merging and Combining Maps in Scala:
Merge and combine two Maps.
val map1 = Map("one" -> 1, "two" -> 2) val map2 = Map("two" -> 20, "three" -> 3) val mergedMap = map1 ++ map2
Checking for Key Existence in Scala Map:
Check if a key exists in a Map.
val myMap = Map("one" -> 1, "two" -> 2, "three" -> 3) val keyExists = myMap.contains("two")
Sorting and Ordering a Scala Map:
Sort and order a Map based on keys or values.
val myMap = Map("one" -> 1, "three" -> 3, "two" -> 2) val sortedByKey = myMap.toSeq.sortBy(_._1).toMap val sortedByValue = myMap.toSeq.sortBy(_._2).toMap
Grouping and Partitioning with Map in Scala:
Group and partition Map entries based on a condition.
val myMap = Map("one" -> 1, "two" -> 2, "three" -> 3, "four" -> 4) val groupedByEvenOdd = myMap.groupBy { case (_, value) => value % 2 == 0 }