Newer Version Available

This content describes an older version of this product. View Latest

Map Class

Contains methods for the Map collection type.

Namespace

System

Usage

The Map methods are all instance methods, that is, they operate on a particular instance of a map. The following are the instance methods for maps.

  • Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
  • Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes. Uniqueness of keys of all other non-primitive types, such as sObject keys, is determined by comparing the objects’ field values.
  • Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct.

Note

For more information on maps, see Maps.

Map Constructors

The following are constructors for Map.

Map<T1,T2>()

Creates a new instance of the Map class. T1 is the data type of the keys and T2 is the data type of the values.

Signature

public Map<T1,T2>()

Example

Map<T1,T2>(mapToCopy)

Creates a new instance of the Map class and initializes it by copying the entries from the specified map. T1 is the data type of the keys and T2 is the data type of the values.

Signature

public Map<T1,T2>(Map<T1,T2> mapToCopy)

Parameters

mapToCopy
Type: Map<T1, T2>
The map to initialize this map with. T1 is the data type of the keys and T2 is the data type of the values. All map keys and values are copied to this map.

Example

Map<ID,sObject>(recordList)

Creates a new instance of the Map class and populates it with the passed-in list of sObject records. The keys are populated with the sObject IDs and the values are the sObjects.

Signature

public Map<ID,sObject>(List<sObject> recordList)

Parameters

recordList
Type: List<sObject>
The list of sObjects to populate the map with.

Example

Map Methods

The following are methods for Map. All are instance methods.

clear()

Removes all of the key-value mappings from the map.

Signature

public Void clear()

Return Value

Type: Void

clone()

Makes a duplicate copy of the map.

Signature

public Map<Object, Object> clone()

Return Value

Type: Map (of same type)

Usage

If this is a map with sObject record values, the duplicate map will only be a shallow copy of the map. That is, the duplicate will have references to each sObject record, but the records themselves are not duplicated. For example:

To also copy the sObject records, you must use the deepClone method.

Example

containsKey(key)

Returns true if the map contains a mapping for the specified key.

Signature

public Boolean containsKey(Object key)

Parameters

key
Type: Object

Return Value

Type: Boolean

Usage

If the key is a string, the key value is case-sensitive.

Example

deepClone()

Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.

Signature

public Map<Object, Object> deepClone()

Return Value

Type: Map (of the same type)

Usage

To make a shallow copy of a map without duplicating the sObject records it contains, use the clone() method.

Example

equals(map2)

Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false.

Signature

public Boolean equals(Map map2)

Parameters

map2
Type: Map
The map2 argument is the map to compare this map with.

Return Value

Type: Boolean

Usage

Two maps are equal if their key/value pairs are identical, regardless of the order of those pairs. The == operator is used to compare the map keys and values.

The == operator is equivalent to calling the equals method, so you can call map1.equals(map2); instead of map1 == map2;.

get(key)

Returns the value to which the specified key is mapped, or null if the map contains no value for this key.

Signature

public Object get(Object key)

Parameters

key
Type: Object

Return Value

Type: Object

Usage

If the key is a string, the key value is case-sensitive.

Example

getSObjectType()

Returns the token of the sObject type that makes up the map values.

Signature

public Schema.SObjectType getSObjectType()

Return Value

Type: Schema.SObjectType

Usage

Use this method with describe information, to determine if a map contains sObjects of a particular type.

Note that this method can only be used with maps that have sObject values.

For more information, see Understanding Apex Describe Information.

Example

hashCode()

Returns the hashcode corresponding to this map.

Signature

public Integer hashCode()

Return Value

Type: Integer

isEmpty()

Returns true if the map has zero key-value pairs.

Signature

public Boolean isEmpty()

Return Value

Type: Boolean

Example

keySet()

Returns a set that contains all of the keys in the map.

Signature

public Set<Object> keySet()

Return Value

Type: Set (of key type)

Example

put(key, value)

Associates the specified value with the specified key in the map.

Signature

public Object put(Object key, Object value)

Parameters

key
Type: Object
value
Type: Object

Return Value

Type: Object

Usage

If the map previously contained a mapping for this key, the old value is returned by the method and then replaced.

If the key is a string, the key value is case-sensitive.

Example

putAll(fromMap)

Copies all of the mappings from the specified map to the original map.

Signature

public Void putAll(Map fromMap)

Parameters

fromMap
Type: Map

Return Value

Type: Void

Usage

The new mappings from fromMap replace any mappings that the original map had.

Example

putAll(sobjectArray)

Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.

Signature

public Void putAll(sObject[] sobjectArray)

Parameters

sobjectArray
Type: sObject[]

Return Value

Type: Void

Usage

This method is similar to calling the Map constructor with the same input.

Example

remove(key)

Removes the mapping for the specified key from the map, if present, and returns the corresponding value.

Signature

public Object remove(Key key)

Parameters

key
Type: Key

Return Value

Type: Object

Usage

If the key is a string, the key value is case-sensitive.

Example

size()

Returns the number of key-value pairs in the map.

Signature

public Integer size()

Return Value

Type: Integer

Example

toString()

Returns the string representation of the map.

Signature

public String toString()

Return Value

Type: String

Usage

When used in cyclic references, the output is truncated to prevent infinite recursion. When used with large collections, the output is truncated to avoid exceeding total heap size and maximum CPU time.
  • Up to 10 items per collection are included in the output, followed by an ellipsis (…).
  • If the same object is included multiple times in a collection, it’s shown in the output only once; subsequent references are shown as (already output).

values()

Returns a list that contains all the values in the map.

Signature

public List<Object> values()

Return Value

Type: List<Object>

Usage

The order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. For example, suppose the values() method returns a list containing value1 and index 0 and value2 and index 1. Subsequent runs of the same code result in those values being returned in the same order.

Example