You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/collections-2.13/maps.md
+85-47Lines changed: 85 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -24,68 +24,87 @@ The fundamental operations on maps are similar to those on sets. They are summar
24
24
25
25
### Operations in Class Map ###
26
26
27
-
| WHAT IT IS | WHAT IT DOES|
28
-
| ------ | ------|
29
-
|**Lookups:**||
30
-
|`msget k`|The value associated with key `k` in map `ms` as an option, `None` if not found.|
31
-
|`ms(k)`|(or, written out, `msapply k`) The value associated with key `k` in map `ms`, or exception if not found.|
32
-
|`msgetOrElse(k, d)`|The value associated with key `k` in map `ms`, or the default value `d` if not found.|
33
-
|`mscontains k`|Tests whether `ms` contains a mapping for key `k`.|
34
-
|`msisDefinedAt k`|Same as `contains`. |
35
-
|**Subcollections:**||
27
+
| WHAT IT IS | WHAT IT DOES|
28
+
| ------ | ------|
29
+
|**Lookups:**||
30
+
|`ms.get(k)`|The value associated with key `k` in map `ms` as an option, `None` if not found.|
31
+
|`ms(k)`|(or, written out, `ms.apply(k)`) The value associated with key `k` in map `ms`, or exception if not found.|
32
+
|`ms.getOrElse(k, d)`|The value associated with key `k` in map `ms`, or the default value `d` if not found.|
33
+
|`ms.contains(k)`|Tests whether `ms` contains a mapping for key `k`.|
34
+
|`ms.isDefinedAt(k)`|Same as `contains`. |
35
+
|**Subcollections:**||
36
36
|`ms.keys`|An iterable containing each key in `ms`. |
37
37
|`ms.keySet`|A set containing each key in `ms`. |
38
38
|`ms.keysIterator`|An iterator yielding each key in `ms`. |
39
39
|`ms.values`|An iterable containing each value associated with a key in `ms`.|
40
40
|`ms.valuesIterator`|An iterator yielding each value associated with a key in `ms`.|
41
-
|**Transformation:**||
42
-
|`ms.viewfilterKeys p`|A map view containing only those mappings in `ms` where the key satisfies predicate `p`.|
43
-
|`ms.viewmapValues f`|A map view resulting from applying function `f` to each value associated with a key in `ms`.|
41
+
|**Transformation:**||
42
+
|`ms.view.filterKeys(p)`|A map view containing only those mappings in `ms` where the key satisfies predicate `p`.|
43
+
|`ms.view.mapValues(f)`|A map view resulting from applying function `f` to each value associated with a key in `ms`.|
44
44
45
45
Immutable maps support in addition operations to add and remove mappings by returning new `Map`s, as summarized in the following table.
46
46
47
47
### Operations in Class immutable.Map ###
48
48
49
-
| WHAT IT IS | WHAT IT DOES|
50
-
| ------ | ------|
51
-
|**Additions and Updates:**||
49
+
| WHAT IT IS | WHAT IT DOES|
50
+
| ------ | ------|
51
+
|**Additions and Updates:**||
52
52
|`ms.updated(k, v)`<br>or `ms + (k -> v)`|The map containing all mappings of `ms` as well as the mapping `k -> v` from key `k` to value `v`.|
53
-
|**Removals:**||
54
-
|`msremove k`<br>or `ms - k`|The map containing all mappings of `ms` except for any mapping of key `k`.|
55
-
|`msremoveAll ks`<br>or `ms -- ks`|The map containing all mappings of `ms` except for any mapping with a key in `ks`.|
53
+
|**Removals:**||
54
+
|`ms.remove(k)`<br>or `ms - k`|The map containing all mappings of `ms` except for any mapping of key `k`.|
55
+
|`ms.removeAll(ks)`<br>or `ms -- ks`|The map containing all mappings of `ms` except for any mapping with a key in `ks`.|
56
56
57
57
Mutable maps support in addition the operations summarized in the following table.
58
58
59
59
60
60
### Operations in Class mutable.Map ###
61
61
62
-
| WHAT IT IS | WHAT IT DOES|
63
-
| ------ | ------|
64
-
|**Additions and Updates:**||
65
-
|`ms(k) = v`|(Or, written out, `ms.update(k, v)`). Adds mapping from key `k` to value `v` to map ms as a side effect, overwriting any previous mapping of `k`.|
66
-
|`ms.addOne(k -> v)`<br>or `ms += (k -> v)`|Adds mapping from key `k` to value `v` to map `ms` as a side effect and returns `ms` itself.|
62
+
| WHAT IT IS | WHAT IT DOES|
63
+
| ------ | ------|
64
+
|**Additions and Updates:**||
65
+
|`ms(k) = v`|(Or, written out, `ms.update(k, v)`). Adds mapping from key `k` to value `v` to map ms as a side effect, overwriting any previous mapping of `k`.|
66
+
|`ms.addOne(k -> v)`<br>or `ms += (k -> v)`|Adds mapping from key `k` to value `v` to map `ms` as a side effect and returns `ms` itself.|
67
67
|`ms addAll xvs`<br>or `ms ++= kvs`|Adds all mappings in `kvs` to `ms` as a side effect and returns `ms` itself.|
68
-
|`ms.put(k, v)`|Adds mapping from key `k` to value `v` to `ms` and returns any value previously associated with `k` as an option.|
69
-
|`msgetOrElseUpdate(k, d)`|If key `k` is defined in map `ms`, return its associated value. Otherwise, update `ms` with the mapping `k -> d` and return `d`.|
70
-
|**Removals:**||
71
-
|`mssubtractOne k`<br>or `ms -= k`|Removes mapping with key `k` from ms as a side effect and returns `ms` itself.|
72
-
|`mssubtractAll ks`<br>or `ms --= ks`|Removes all keys in `ks` from `ms` as a side effect and returns `ms` itself.|
73
-
|`msremove k`|Removes any mapping with key `k` from `ms` and returns any value previously associated with `k` as an option.|
74
-
|`msfilterInPlace p`|Keeps only those mappings in `ms` that have a key satisfying predicate `p`.|
75
-
|`ms.clear()`|Removes all mappings from `ms`. |
76
-
|**Transformation:**||
77
-
|`msmapValuesInPlace f`|Transforms all associated values in map `ms` with function `f`.|
78
-
|**Cloning:**||
79
-
|`ms.clone`|Returns a new mutable map with the same mappings as `ms`.|
68
+
|`ms.put(k, v)`|Adds mapping from key `k` to value `v` to `ms` and returns any value previously associated with `k` as an option.|
69
+
|`ms.getOrElseUpdate(k, d)`|If key `k` is defined in map `ms`, return its associated value. Otherwise, update `ms` with the mapping `k -> d` and return `d`.|
70
+
|**Removals:**||
71
+
|`ms.subtractOne(k)`<br>or `ms -= k`|Removes mapping with key `k` from ms as a side effect and returns `ms` itself.|
72
+
|`ms.subtractAll(ks)`<br>or `ms --= ks`|Removes all keys in `ks` from `ms` as a side effect and returns `ms` itself.|
73
+
|`ms.remove(k)`|Removes any mapping with key `k` from `ms` and returns any value previously associated with `k` as an option.|
74
+
|`ms.filterInPlace(p)`|Keeps only those mappings in `ms` that have a key satisfying predicate `p`.|
75
+
|`ms.clear()`|Removes all mappings from `ms`. |
76
+
|**Transformation:**||
77
+
|`ms.mapValuesInPlace(f)`|Transforms all associated values in map `ms` with function `f`.|
78
+
|**Cloning:**||
79
+
|`ms.clone`|Returns a new mutable map with the same mappings as `ms`.|
80
80
81
81
The addition and removal operations for maps mirror those for sets. A mutable map `m` is usually updated "in place", using the two variants `m(key) = value` or `m += (key -> value)`. There is also the variant `m.put(key, value)`, which returns an `Option` value that contains the value previously associated with `key`, or `None` if the `key` did not exist in the map before.
82
82
83
83
The `getOrElseUpdate` is useful for accessing maps that act as caches. Say you have an expensive computation triggered by invoking a function `f`:
Assume further that `f` has no side-effects, so invoking it again with the same argument will always yield the same result. In that case you could save time by storing previously computed bindings of argument and results of `f` in a map and only computing the result of `f` if a result of an argument was not found there. One could say the map is a _cache_ for the computations of the function `f`.
91
110
@@ -94,7 +113,7 @@ Assume further that `f` has no side-effects, so invoking it again with the same
94
113
95
114
You can now create a more efficient caching version of the `f` function:
@@ -104,10 +123,29 @@ You can now create a more efficient caching version of the `f` function:
104
123
105
124
Note that the second argument to `getOrElseUpdate` is "by-name", so the computation of `f("abc")` above is only performed if `getOrElseUpdate` requires the value of its second argument, which is precisely if its first argument is not found in the `cache` map. You could also have implemented `cachedF` directly, using just basic map operations, but it would take more code to do so:
106
125
107
-
def cachedF(arg: String) = cache get arg match {
108
-
case Some(result) => result
109
-
case None =>
110
-
val result = f(x)
111
-
cache(arg) = result
112
-
result
113
-
}
126
+
{% tabs cacheF class=tabs-scala-version %}
127
+
128
+
{% tab 'Scala 2' for=cacheF %}
129
+
```scala
130
+
defcachedF(arg: String):String= cache.get(arg) match {
131
+
caseSome(result) => result
132
+
caseNone=>
133
+
valresult= f(x)
134
+
cache(arg) = result
135
+
result
136
+
}
137
+
```
138
+
{% endtab %}
139
+
140
+
{% tab 'Scala 3' for=cacheF %}
141
+
```scala
142
+
defcachedF(arg: String):String= cache.get(arg) match
0 commit comments