Map Class
Contains methods for the Map collection type.
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
equalsandhashCodemethods, 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. Use caution when you use an sObject as a map key because when the sObject is changed, it no longer maps to the same value. For information and examples, see sObject Map Considerations. - 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, andremovetreat these keys as distinct. - With the
keySet()method, the returned keySet is backed by the map and reflects any changes made to the map, and vice versa.
For more information on maps, see Maps.
The following are constructors for Map.
-
Creates a new instance of the
Mapclass. T1 is the data type of the keys and T2 is the data type of the values. -
Creates a new instance of the
Mapclass 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. -
Creates a new instance of the
Mapclass 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.
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.
public Map<T1,T2>()
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.
public Map<T1,T2>(Map<T1,T2> mapToCopy)
- 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.
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.
public Map<ID,sObject>(List<sObject> recordList)
- recordList: Type: List<sObject> The list of sObjects to populate the map with.
The following are methods for Map. All are instance methods.
-
Removes all of the key-value mappings from the map.
-
Makes a duplicate copy of the map.
-
Returns
trueif the map contains a mapping for the specified key. -
Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.
-
Compares this map with the specified map and returns
trueif both maps are equal; otherwise, returnsfalse. -
Returns the value to which the specified key is mapped, or
nullif the map contains no value for this key. -
Returns the token of the sObject type that makes up the map values.
-
Returns the hashcode corresponding to this map.
-
Returns true if the map has zero key-value pairs.
-
Returns a set that contains all of the keys in the map.
-
Associates the specified value with the specified key in the map.
-
Copies all of the mappings from the specified map to the original map.
-
Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
-
Removes the mapping for the specified key from the map, if present, and returns the corresponding value.
-
Returns the number of key-value pairs in the map.
-
Returns the string representation of the map.
-
Returns a list that contains all the values in the map.
Removes all of the key-value mappings from the map.
public Void clear()
Type: Void
Makes a duplicate copy of the map.
public Map<Object, Object> clone()
Type: Map (of same type)
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.
Returns true if the map contains a mapping for the specified key.
public Boolean containsKey(Object key)
- key: Type: Object
Type: Boolean
If the key is a string, the key value is case-sensitive.
Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.
public Map<Object, Object> deepClone()
Type: Map (of the same type)
To make a shallow copy of a map without duplicating the sObject records it contains, use the clone() method.
Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false.
public Boolean equals(Map map2)
- map2: Type: Map The map2 argument is the map to compare this map with.
Type: Boolean
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;.
Returns the value to which the specified key is mapped, or null if the map contains no value for this key.
public Object get(Object key)
- key: Type: Object
Type: Object
If the key is a string, the key value is case-sensitive.
Returns the token of the sObject type that makes up the map values.
public Schema.SObjectType getSObjectType()
Type: Schema.SObjectType
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.
Returns the hashcode corresponding to this map.
public Integer hashCode()
Type: Integer
Returns true if the map has zero key-value pairs.
public Boolean isEmpty()
Type: Boolean
Returns a set that contains all of the keys in the map.
public Set<Object> keySet()
Type: Set (of key type)
The returned keySet is backed by the map, so the keySet reflects any changes made to the map, and vice-versa.
Associates the specified value with the specified key in the map.
public Object put(Object key, Object value)
- key: Type: Object
- value: Type: Object
Type: Object
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.
Copies all of the mappings from the specified map to the original map.
public Void putAll(Map fromMap)
- fromMap: Type: Map
Type: Void
The new mappings from fromMap are merged with any mappings that existed in the original map. If any of the keys match, the original map values are replaced by corresponding values in the new mapping.
Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
public Void putAll(sObject[] sobjectArray)
- sobjectArray: Type: sObject[]
Type: Void
This method is similar to calling the Map constructor with the same input.
Removes the mapping for the specified key from the map, if present, and returns the corresponding value.
public Object remove(Key key)
- key: Type: Key
Type: Object
If the key is a string, the key value is case-sensitive.
Returns the number of key-value pairs in the map.
public Integer size()
Type: Integer
Returns the string representation of the map.
public String toString()
Type: String
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).
Returns a list that contains all the values in the map.
public List<Object> values()
Type: List<Object>
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.