scala.collection.mutable

trait MapLike

[source: scala/collection/mutable/MapLike.scala]

trait MapLike[A, B, +This <: MapLike[A, B, This] with Map[A, B]]
extends MapLikeBase[A, B, This] with Builder[(A, B), This] with Shrinkable[A]

A generic template for mutable maps from keys of type A to values of type B.

To implement a concrete mutable map, you need to provide implementations of the following methods:

    def get(key: A): Option[B]
    def iterator: Iterator[(A, B)]
    def += (kv: (A, B)): this.type
    def -= (key: A): this.type

If you wish that methods like, take, drop, filter return the same kind of map, you should also override:

   def empty: This

If you to avoid the unncessary construction of an Option object, you could also override apply, update, and delete.

It is also good idea to override methods foreach and size for efficiency.

Since
2.8
Direct Known Subclasses:
JavaConversions.JMapWrapper, HashMap, LinkedHashMap, ListMap, Map

Method Summary
def + (kv : (A, B)) : MapLike[A, B, This]
Add a new key/value mapping and return the map itself.
def + (elem1 : (A, B), elem2 : (A, B), elems : (A, B)*) : MapLike[A, B, This]
Adds two or more key/value mappings and return the map itself. with the added elements.
def ++ (iter : Iterator[(A, B)]) : MapLike[A, B, This]
Adds a number of elements provided by an iterator via its iterator method and returns the collection itself.
def ++ (iter : Traversable[(A, B)]) : MapLike[A, B, This]
Adds a number of elements provided by a traversable object via its iterator method and returns either the collection itself (if it is mutable), or a new collection with the added elements.
abstract def += (kv : (A, B)) : MapLike[A, B, This]
Add a new key/value mapping this map.
override def - (elem1 : A, elem2 : A, elems : A*) : This
Removes two or more elements from this collection and returns the collection itself.
override def - (key : A) : This
Delete a key from this map if it is present and return the map itself.
override def -- (iter : Iterator[A]) : This
Removes a number of elements provided by an iterator and returns the collection itself.
override def -- (iter : Traversable[A]) : This
Removes a number of elements provided by a Traversable object and returns the collection itself.
abstract def -= (key : A) : MapLike[A, B, This]
Delete a key from this map if it is present.
def cached (key : A, op : => B) : B
If given key is already in this map, returns associated value Otherwise, computes value from given expression `op`, stores with key in map and returns that value.
def clear : Unit
Removes all elements from the set. After this operation is completed, the set will be empty.
override def clone : This
This method creates and returns a copy of the receiver object.
def getOrElseUpdate (key : A, default : => B) : B
Check if this map maps key to a value. Return that value if it exists, otherwise put default as that key's value and return it.
protected[this] override def newBuilder : Builder[(A, B), This]
def put (key : A, value : B) : Option[B]
Adds a new mapping from key to value to the map. If the map already contains a mapping for key, it will be overridden.
def remove (key : A) : Option[B]
If given key is defined in this map, remove it and return associated value as an Option. If key is not present return None.
def removeKey (key : A) : Option[B]
If given key is defined in this map, remove it and return associated value as an Option. If key is not present return None.
def result : This
The result when this map is used as a builder
def retain (p : (A, B) => Boolean) : MapLike[A, B, This]
Retain only those mappings for which the predicate p returns true.
def transform (f : (A, B) => B) : MapLike[A, B, This]
This function transforms all the values of mappings contained in this map with function f.
def update (key : A, value : B) : Unit
Adds a new mapping from key to value to the map. If the map already contains a mapping for key, it will be overridden.
override def updated [B1 >: B](key : A, value : B1) : Map[A, B1]
Create a new map consisting of all elements of the current map plus the given mapping from key to value.
Methods inherited from Shrinkable
-=, --=, --=
Methods inherited from Builder
sizeHint, mapResult
Methods inherited from Growable
+=, ++=, ++=
Methods inherited from MapLikeBase
+
Methods inherited from MapLike
empty (abstract), get (abstract), iterator (abstract), isEmpty, getOrElse, apply, contains, isDefinedAt, keySet, keysIterator, keys, valuesIterable, valuesIterator, values, default, filterKeys, mapValues, mapElements, +, ++, ++, addString, stringPrefix, toString, hashCode, equals
Methods inherited from IterableLike
thisCollection, toCollection, elements, foreach, forall, exists, find, foldRight, reduceRight, toIterable, head, take, slice, takeWhile, takeRight, dropRight, copyToArray, zip, zipAll, zipWithIndex, sameElements, toStream, canEqual, view, view, first, firstOption, projection
Methods inherited from TraversableLike
repr, nonEmpty, size, hasDefiniteSize, ++, ++, map, flatMap, filter, filterNot, partialMap, remove, partition, groupBy, count, foldLeft, /:, :\, reduceLeft, reduceLeftOption, reduceRightOption, sum, product, min, max, headOption, tail, last, lastOption, init, drop, dropWhile, span, splitAt, copyToBuffer, copyToArray, toArray, toList, toSeq, toIndexedSeq, toSet, mkString, mkString, mkString, addString, addString, withFilter
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from AnyRef
getClass, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
protected[this] override def newBuilder : Builder[(A, B), This]

A common implementation of newBuilder for all mutable maps in terms of empty.

Overrides MapLike implementation for better efficiency.


def put(key : A, value : B) : Option[B]
Adds a new mapping from key to value to the map. If the map already contains a mapping for key, it will be overridden.
Parameters
key - The key to update
value - The new value

def update(key : A, value : B) : Unit
Adds a new mapping from key to value to the map. If the map already contains a mapping for key, it will be overridden.
Parameters
key - The key to update
value - The new value
Returns
An option consisting of value associated previously associated with `key` in the map, or None if `key` was not yet defined in the map.

abstract def +=(kv : (A, B)) : MapLike[A, B, This]
Add a new key/value mapping this map.
Parameters
kv - the key/value pair.
Returns
the map itself
Overrides
Builder.+=

override def updated[B1 >: B](key : A, value : B1) : Map[A, B1]
Create a new map consisting of all elements of the current map plus the given mapping from key to value.
Parameters
key - The key to ad
value - The new value
Returns
A fresh immutable map

def cached(key : A, op : => B) : B
If given key is already in this map, returns associated value Otherwise, computes value from given expression `op`, stores with key in map and returns that value.

@deprecated("This operation will create a new map in the future. To add an element as a side\012".+("effect to an existing map and return that map itself, use +=. If you do want\012").+("to create a fresh map, you can use `clone() +=' to avoid a @deprecated warning."))

def +(kv : (A, B)) : MapLike[A, B, This]
Add a new key/value mapping and return the map itself.
Parameters
kv - the key/value mapping to be added

@deprecated("This operation will create a new map in the future. To add an element as a side\012".+("effect to an existing map and return that map itself, use +=. If you do want to\012").+("create a fresh map, you can use `clone() +=` to avoid a @deprecated warning."))

def +(elem1 : (A, B), elem2 : (A, B), elems : (A, B)*) : MapLike[A, B, This]
Adds two or more key/value mappings and return the map itself. with the added elements.
Parameters
elem1 - the first element to add.
elem2 - the second element to add.
elems - the remaining elements to add.

@deprecated("This operation will create a new map in the future. To add elements as a side\012".+("effect to an existing map and return that map itself, use ++=. If you do want\012").+("to create a fresh map, you can use `clone() ++=` to avoid a @deprecated warning."))

def ++(iter : Traversable[(A, B)]) : MapLike[A, B, This]
Adds a number of elements provided by a traversable object via its iterator method and returns either the collection itself (if it is mutable), or a new collection with the added elements.
Parameters
iter - the traversable object.

@deprecated("This operation will create a new map in the future. To add elements as a side\012".+("effect to an existing map and return that map itself, use ++=. If you do want\012").+("to create a fresh map, you can use `clone() +=` to avoid a @deprecated warning."))

def ++(iter : Iterator[(A, B)]) : MapLike[A, B, This]
Adds a number of elements provided by an iterator via its iterator method and returns the collection itself.
Parameters
iter - the iterator

def remove(key : A) : Option[B]
If given key is defined in this map, remove it and return associated value as an Option. If key is not present return None.
Parameters
key - the key to be removed

abstract def -=(key : A) : MapLike[A, B, This]
Delete a key from this map if it is present.
Parameters
key - the key to be removed
Notes
same as `delete`.
Overrides
Shrinkable.-=

@deprecated("This operation will create a new map in the future. To add elements as a side\012".+("effect to an existing map and return that map itself, use -=. If you do want\012").+("to create a fresh map, you can use `clone() -=` to avoid a @deprecated warning."))

override def -(key : A) : This
Delete a key from this map if it is present and return the map itself.
Parameters
key - the key to be removed

@deprecated("Use `remove' instead")

def removeKey(key : A) : Option[B]
If given key is defined in this map, remove it and return associated value as an Option. If key is not present return None.
Parameters
key - the key to be removed

def clear : Unit
Removes all elements from the set. After this operation is completed, the set will be empty.
Overrides
Builder.clear

def getOrElseUpdate(key : A, default : => B) : B
Check if this map maps key to a value. Return that value if it exists, otherwise put default as that key's value and return it.

def transform(f : (A, B) => B) : MapLike[A, B, This]
This function transforms all the values of mappings contained in this map with function f.
Parameters
f - The transformation to apply

@deprecated("cannot be type inferred because of retain in Iterable.")

def retain(p : (A, B) => Boolean) : MapLike[A, B, This]
Retain only those mappings for which the predicate p returns true.
Parameters
p - The test predicate

override def clone : This
This method creates and returns a copy of the receiver object.

The default implementation of the clone method is platform dependent.

Returns
a copy of the receiver object.


def result : This
The result when this map is used as a builder
Overrides
Builder.result

@deprecated("Use -= instead if you intend to remove by side effect from an existing collection.\012".+("Use `clone() -=' if you intend to create a new collection."))

override def -(elem1 : A, elem2 : A, elems : A*) : This
Removes two or more elements from this collection and returns the collection itself.
Parameters
elem1 - the first element to remove.
elem2 - the second element to remove.
elems - the remaining elements to remove.

@deprecated("Use --= instead if you intend to remove by side effect from an existing collection.\012".+("Use `clone() --=' if you intend to create a new collection."))

override def --(iter : Traversable[A]) : This
Removes a number of elements provided by a Traversable object and returns the collection itself.
Parameters
iter - the Traversable object.

@deprecated("Use --= instead if you intend to remove by side effect from an existing collection.\012".+("Use `clone() --=' if you intend to create a new collection."))

override def --(iter : Iterator[A]) : This
Removes a number of elements provided by an iterator and returns the collection itself.
Parameters
iter - the iterator