Newer Version Available
Map Class
Namespace
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.
For more information on maps, see Maps.
Map Constructors
The following are constructors for Map.
Map<T1,T2>()
Signature
public Map<T1,T2>()
Example
Map<T1,T2>(mapToCopy)
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)
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()
Signature
public Void clear()
Return Value
Type: Void
clone()
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)
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()
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)
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)
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()
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()
Signature
public Integer hashCode()
Return Value
Type: Integer
isEmpty()
Signature
public Boolean isEmpty()
Return Value
Type: Boolean
Example
keySet()
Signature
public Set<Object> keySet()
Return Value
Type: Set (of key type)
Example
put(key, value)
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)
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)
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)
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()
Signature
public Integer size()
Return Value
Type: Integer
Example
toString()
Signature
public String toString()
Return Value
Type: String
Usage
- 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()
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.