scala.collection

trait Map

[source: scala/collection/Map.scala]

trait Map[A, +B]
extends PartialFunction[A, B] with Collection[(A, B)]

A map is a collection that maps each key to one or zero values.

This trait provides a limited interface, only allowing reading of elements. There are two extensions of this trait, in packages scala.collection.mutable and scala.collection.immutable, which provide functionality for adding new key/value mappings to a map. The trait in the first package is for maps that are modified destructively, whereas the trait in the second package is for immutable maps which create a new map when something is added or removed from them.

Author
Matthias Zenger
Martin Odersky
Version
1.2, 31/12/2006
Direct Known Subclasses:
Map.Projection, MapProxy, SortedMap, Map, Map

Method Summary
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.
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) : Projection[A, B]
non-strict filter based on keys only
abstract def get (key : A) : Option[B]
Check if this map maps key to a value and return the value if it exists.
def getOrElse [B2 >: B](key : A, default : => B2) : B2
Check if this map maps key to a value. Return that value if it exists, otherwise return default.
override def hashCode : Int
A hash method compatible with equals
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?
def keySet : Set[A]
def keys : Iterator[A]
Creates an iterator for all keys.
def mapElements [C](f : (B) => C) : Projection[A, C]
non-strict map elements using existing key set
override def projection : Projection[A, B]
returns a projection that can be used to call non-strict filter, map, and flatMap methods that build projections of the collection.
abstract def size : Int
Compute the number of key-to-value mappings.
protected override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation.
override def toString : java.lang.String
Creates a string representation for this map.
def values : Iterator[B]
Creates an iterator for a contained values.
Methods inherited from Collection
toArray
Methods inherited from Iterable
elements (abstract), concat, ++, map, flatMap, filter, partition, takeWhile, dropWhile, take, drop, foreach, forall, exists, find, findIndexOf, indexOf, foldLeft, foldRight, /:, :\, reduceLeft, reduceRight, copyToBuffer, sameElements, toList, toStream, mkString, mkString, mkString, addString, addString, copyToArray, hasDefiniteSize
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
Method Details
abstract def size : Int
Compute the number of key-to-value mappings.
Returns
the number of mappings
Overrides
Collection.size

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

def getOrElse[B2 >: B](key : A, default : => B2) : B2
Check if this map maps key to a value. Return that value if it exists, otherwise return default.

override def isEmpty : Boolean
Is this an empty map?
Returns
true iff the map is empty.

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 keys : Iterator[A]
Creates an iterator for all keys.
Returns
an iterator over all keys.

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

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

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 hashCode : Int
A hash method compatible with equals

override def toString : java.lang.String
Creates a string representation for this map.
Returns
a string showing all mappings
Overrides
Collection.toString

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 -

override def projection : Projection[A, B]
returns a projection that can be used to call non-strict filter, map, and flatMap methods that build projections of the collection.

def filterKeys(p : (A) => Boolean) : Projection[A, B]
non-strict filter based on keys only

def mapElements[C](f : (B) => C) : Projection[A, C]
non-strict map elements using existing key set

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