Scala Map

Posted By admin On 28/04/18

A is an consisting of pairs of keys and values (also named mappings or associations). Scala’s object offers an implicit conversion that lets you write key ->value as an alternate syntax for the pair (key, value). For instance Map('x' ->24, 'y' ->25, 'z' ->26) means exactly the same as Map(('x', 24), ('y', 25), ('z', 26)), but reads better. The fundamental operations on maps are similar to those on sets.

Scala Map

They are summarized in the following table and fall into the following categories: • Lookup operations apply, get, getOrElse, contains, and isDefinedAt. These turn maps into partial functions from keys to values.

Iterating over Map with Scala. Here is a summary of the iteration over a Scala Map discussion, started by Ikai at the Scala mailing list. Get directions, reviews and information for La Scala in Baltimore, MD.

The fundamental lookup method for a map is: def get(key): Option[Value]. The operation “ m get key” tests whether the map contains an association for the given key. Giren No Yabou Axis No Kyoui Iso here. If so, it returns the associated value in a Some.

If no key is defined in the map, get returns None. Maps also define an apply method that returns the value associated with a given key directly, without wrapping it in an Option. If the key is not defined in the map, an exception is raised. • Additions and updates +, ++, updated, which let you add new bindings to a map or change existing bindings. • Removals -, --, which remove bindings from a map. • Subcollection producers keys, keySet, keysIterator, values, valuesIterator, which return a map’s keys and values separately in various forms. • Transformations filterKeys and mapValues, which produce a new map by filtering and transforming bindings of an existing map.

Operations in Class Map WHAT IT IS WHAT IT DOES Lookups: ms get k The value associated with key k in map ms as an option, None if not found. Ms(k) (or, written out, ms apply k) The value associated with key k in map ms, or exception if not found. Ms getOrElse (k, d) The value associated with key k in map ms, or the default value d if not found. Ms contains k Tests whether ms contains a mapping for key k. Ms isDefinedAt k Same as contains. Additions and Updates: ms + (k ->v) The map containing all mappings of ms as well as the mapping k ->v from key k to value v.

Ms + (k ->v, l ->w) The map containing all mappings of ms as well as the given key/value pairs. Ms ++ kvs The map containing all mappings of ms as well as all key/value pairs of kvs. Ms updated (k, v) Same as ms + (k ->v). Removals: ms - k The map containing all mappings of ms except for any mapping of key k. Ms - (k, l, m) The map containing all mappings of ms except for any mapping with the given keys. Ms -- ks The map containing all mappings of ms except for any mapping with a key in ks.

Subcollections: ms.keys An iterable containing each key in ms. Ms.keySet A set containing each key in ms. Ms.keyIterator An iterator yielding each key in ms. Ms.values An iterable containing each value associated with a key in ms.

Ms.valuesIterator An iterator yielding each value associated with a key in ms. Transformation: ms filterKeys p A map view containing only those mappings in ms where the key satisfies predicate p.

Ms mapValues f A map view resulting from applying function f to each value associated with a key in ms. Mutable maps support in addition the operations summarized in the following table. Operations in Class mutable.Map WHAT IT IS WHAT IT DOES Additions and Updates: ms(k) = v (Or, written out, ms.update(x, v)). Adds mapping from key k to value v to map ms as a side effect, overwriting any previous mapping of k. Ms += (k ->v) Adds mapping from key k to value v to map ms as a side effect and returns ms itself. Ms += (k ->v, l ->w) Adds the given mappings to ms as a side effect and returns ms itself. Ms ++= kvs Adds all mappings in kvs to ms as a side effect and returns ms itself.

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. 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.

Removals: ms -= k Removes mapping with key k from ms as a side effect and returns ms itself. Ms -= (k, l, m) Removes mappings with the given keys from ms as a side effect and returns ms itself. Ms --= ks Removes all keys in ks from ms as a side effect and returns ms itself. Ms remove k Removes any mapping with key k from ms and returns any value previously associated with k as an option. Ms retain p Keeps only those mappings in ms that have a key satisfying predicate p. Ms.clear() Removes all mappings from ms.