scala.collection.generic

trait MapTemplate

[source: scala/collection/generic/MapTemplate.scala]

trait MapTemplate[A, +B, +This <: MapTemplate[A, B, This] with Map[A, B]]
extends PartialFunction[A, B] with IterableTemplate[(A, B), This] with Subtractable[A, This]

A generic template for maps from keys of type A to values of type B.
To implement a concrete map, you need to provide implementations of the following methods (where This is the type of the map in question):

    def get(key: A): Option[B]
    def iterator: Iterator[(A, B)]
    def + [B1 >: B](kv: (A, B1)): This
    def -(key: A): This

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

   def empty: This

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

Author
Martin Odersky
Version
2.8
Direct Known Subclasses:
Map, ImmutableMapTemplate, MapProxyTemplate, MutableMapTemplateBase, SortedMapTemplate

Method Summary
def + [B1 >: B](elem1 : (A, B1), elem2 : (A, B1), elems : (A, B1)*) : Map[A, B1]
Adds two or more elements to this collection and returns a new collection.
abstract def + [B1 >: B](kv : (A, B1)) : Map[A, B1]
Add a key/value pair to this map, returning a new map.
def ++ [B1 >: B](elems : Traversable[(A, B1)]) : Map[A, B1]
Adds a number of elements provided by a traversable object and returns a new collection with the added elements.
def ++ [B1 >: B](iter : Iterator[(A, B1)]) : Map[A, B1]
Adds a number of elements provided by an iterator and returns a new collection with the added elements.
abstract def - (key : A) : This
Removes a key from this map, returning a new map
override def addString (b : StringBuilder, start : java.lang.String, sep : java.lang.String, end : java.lang.String) : StringBuilder
Creates a string representation for this map.
def apply (key : A) : B
Retrieve the value which is associated with the given key. This method throws an exception if there is no mapping from the given key to a value.
def contains (key : A) : Boolean
Is the given key mapped to a value by this map?
def default (key : A) : B
The default value for the map, returned when a key is not found The method implemented here yields an error, but it might be overridden in subclasses.
abstract def empty : This
override def equals (that : Any) : Boolean
Compares two maps structurally; i.e. checks if all mappings contained in this map are also contained in the other map, and vice versa.
def filterKeys (p : (A) => Boolean) : DefaultMap[A, B]
A map view consisting only of those key value pairs where the key satisfies a given predicate `p`.
abstract def get (key : A) : Option[B]
Check if this map maps key to a value and return the value as an option if it exists, None if not.
def getOrElse [B1 >: B](key : A, default : => B1) : B1
Check if this map maps key to a value. Return that value if it exists, otherwise return default.
def isDefinedAt (key : A) : Boolean
Does this map contain a mapping from the given key to a value?
override def isEmpty : Boolean
Is this an empty map?
abstract def iterator : Iterator[(A, B)]
An iterator yielding all key/value mappings of this map.
def keySet : Set[A]
def keys : Iterator[A]
Creates an iterator for all keys.
def keysIterator : Iterator[A]
Creates an iterator for all keys.
def mapElements [C](f : (B) => C) : DefaultMap[A, C]
def mapValues [C](f : (B) => C) : DefaultMap[A, C]
A map view resulting from applying a given function `f` to each value associated with a key in this map.
protected[this] override def newBuilder : Builder[(A, B), This]
A common implementation of `newBuilder` for all maps in terms of `empty`. Overridden for mutable maps in `MutableMapTemplate`.
override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation.
override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.
def updated [B1 >: B](key : A, value : B1) : Map[A, B1]
A new immutable map containing updating this map with a given key/value mapping.
def valueIterable : Iterable[B]
def values : Iterator[B]
Creates an iterator for a contained values.
def valuesIterator : Iterator[B]
Creates an iterator for a contained values.
Methods inherited from Subtractable
-, --, --
Methods inherited from IterableTemplate
elements, foreach, foldRight, reduceRight, toIterable, head, takeRight, dropRight, sameElements, toStream, view, view, first, firstOption, toSeq, projection
Methods inherited from TraversableTemplate
thisCollection, nonEmpty, size, hasDefiniteSize, ++, ++, map, flatMap, filter, filterMap, filterNot, remove, partition, groupBy, forall, exists, count, find, foldLeft, /:, :\, reduceLeft, reduceLeftOption, reduceRightOption, headOption, tail, last, lastOption, init, take, drop, slice, takeWhile, dropWhile, span, splitAt, copyToBuffer, copyToArray, copyToArray, toArray, toList, toSequence, toSet, mkString, mkString, mkString, addString, addString
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from AnyRef
getClass, hashCode, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Class Summary
protected class DefaultKeySet extends Set[A]
protected class DefaultValuesIterable extends Iterable[B]
Method Details
abstract def empty : This

protected[this] override def newBuilder : Builder[(A, B), This]
A common implementation of `newBuilder` for all maps in terms of `empty`. Overridden for mutable maps in `MutableMapTemplate`.

abstract def get(key : A) : Option[B]
Check if this map maps key to a value and return the value as an option if it exists, None if not.
Parameters
key - the key of the mapping of interest.
Returns
the value of the mapping as an option, if it exists, or None.

abstract def iterator : Iterator[(A, B)]
An iterator yielding all key/value mappings of this map.
Overrides
IterableTemplate.iterator

abstract def +[B1 >: B](kv : (A, B1)) : Map[A, B1]
Add a key/value pair to this map, returning a new map.
Parameters
kv - the key/value pair
Returns
A new map with the new binding added to this map

abstract def -(key : A) : This
Removes a key from this map, returning a new map
Parameters
key - the key to be removed
Returns
A new map without a binding for key
Overrides
Subtractable.-

override def isEmpty : Boolean
Is this an empty map?
Returns
true iff the map does not contain any key/value mapping.
Overrides
IterableTemplate.isEmpty

def getOrElse[B1 >: B](key : A, default : => B1) : B1
Check if this map maps key to a value. Return that value if it exists, otherwise return default.
Parameters
key - the key.
default - a computation that yields a default value in case no binding for the key is found in the map.

def apply(key : A) : B
Retrieve the value which is associated with the given key. This method throws an exception if there is no mapping from the given key to a value.
Parameters
key - the key
Returns
the value associated with the given key.

def contains(key : A) : Boolean
Is the given key mapped to a value by this map?
Parameters
key - the key
Returns
true iff there is a mapping for key in this map

def isDefinedAt(key : A) : Boolean
Does this map contain a mapping from the given key to a value?
Parameters
key - the key
Returns
true iff there is a mapping for key in this map
Overrides
PartialFunction.isDefinedAt

def keySet : Set[A]
Returns
the keys of this map as a set.

def keysIterator : Iterator[A]
Creates an iterator for all keys.
Returns
an iterator over all keys.

@deprecated("use `keysIterator' instead")

def keys : Iterator[A]
Creates an iterator for all keys.
Returns
an iterator over all keys.

def valueIterable : Iterable[B]
Returns
the values of this map as an iterable.

def valuesIterator : Iterator[B]
Creates an iterator for a contained values.
Returns
an iterator over all values.

@deprecated("use `valuesIterator' instead")

def values : Iterator[B]
Creates an iterator for a contained values.
Returns
an iterator over all values.

def default(key : A) : B
The default value for the map, returned when a key is not found The method implemented here yields an error, but it might be overridden in subclasses.
Parameters
key - the given key value
Throws
Predef.NoSuchElementException -

def filterKeys(p : (A) => Boolean) : DefaultMap[A, B]
A map view consisting only of those key value pairs where the key satisfies a given predicate `p`.

def mapValues[C](f : (B) => C) : DefaultMap[A, C]
A map view resulting from applying a given function `f` to each value associated with a key in this map.

@deprecated("use `mapValues' instead")

def mapElements[C](f : (B) => C) : DefaultMap[A, C]

def updated[B1 >: B](key : A, value : B1) : Map[A, B1]
A new immutable map containing updating this map with a given key/value mapping.
Parameters
key - the key
value - the value
Returns
A new map with the new key/value mapping

def +[B1 >: B](elem1 : (A, B1), elem2 : (A, B1), elems : (A, B1)*) : Map[A, B1]
Adds two or more elements to this collection and returns a new collection.
Parameters
elem1 - the first element to add.
elem2 - the second element to add.
elems - the remaining elements to add.

def ++[B1 >: B](elems : Traversable[(A, B1)]) : Map[A, B1]
Adds a number of elements provided by a traversable object and returns a new collection with the added elements.
Parameters
elems - the traversable object.

def ++[B1 >: B](iter : Iterator[(A, B1)]) : Map[A, B1]
Adds a number of elements provided by an iterator and returns a new collection with the added elements.
Parameters
iter - the iterator

override def addString(b : StringBuilder, start : java.lang.String, sep : java.lang.String, end : java.lang.String) : StringBuilder
Creates a string representation for this map.
Returns
a string showing all mappings

override def equals(that : Any) : Boolean
Compares two maps structurally; i.e. checks if all mappings contained in this map are also contained in the other map, and vice versa.
Parameters
that - the other map
Returns
true iff both maps contain exactly the same mappings.

override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation.

override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.