scala.collection

trait MapLike

[source: scala/collection/MapLike.scala]

trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]]
extends PartialFunction[A, B] with IterableLike[(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
Since
2.8
Direct Known Subclasses:
Map, MapProxyLike, SortedMapLike, MapLike, MapLikeBase

Method Summary
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](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.
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.
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.
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.
override def hashCode : Int
Returns a hash code value for the object.
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 `mutable.MapLike`.
override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation. !!! todo: remove stringPrefix overrides where possible
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 values : Iterator[B]
Creates an iterator for a contained values.
def valuesIterable : Iterable[B]
def valuesIterator : Iterator[B]
Creates an iterator for a contained values.
Methods inherited from Subtractable
-, --, --
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, 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 `mutable.MapLike`.

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
IterableLike.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
IterableLike.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 valuesIterable : 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 stringPrefix : java.lang.String
Defines the prefix of this object's toString representation. !!! todo: remove stringPrefix overrides where possible

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

override def hashCode : Int
Returns a hash code value for the object.

The default hashing algorithm is platform dependent. Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

Returns
the hash code value for the object.


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.