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.
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| clear | Void | Removes all of the key-value mappings from the map. | |
| clone | Map (of same type) | Makes a duplicate copy of the map. Note that 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: Account a = new Account( Name='Acme', BillingCity='New York'); Map<Integer, Account> map1 = new Map<Integer, Account> {}; map1.put(1, a); Map<Integer, Account> map2 = map1.clone(); map1.get(1).BillingCity = 'San Francisco'; System.assertEquals( map1.get(1).BillingCity, 'San Francisco'); System.assertEquals( map2.get(1).BillingCity, 'San Francisco'); To also copy the sObject records, you must use the deepClone method. |
|
| containsKey | Key type key | Boolean | Returns true if the map contains a mapping for the specified key. If the key is a string, the key value is case-sensitive. For example: Map<string, string> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); Boolean contains = colorCodes.containsKey('Blue'); System.assertEquals(contains, True); |
| deepClone | Map (of the same type) | Makes a duplicate copy of a map, including sObject records
if this is a map with sObject record values. For example:Account a = new Account( Name='Acme', BillingCity='New York'); Map<Integer, Account> map1 = new Map<Integer, Account> {}; map1.put(1, a); Map<Integer, Account> map2 = map1.deepClone(); map1.get(1).BillingCity = 'San Francisco'; System.assertEquals(map1.get(1). BillingCity, 'San Francisco'); System.assertEquals(map2.get(1). BillingCity, 'New York'); To make a shallow copy of a map without duplicating the sObject records it contains, use the clone() method. |
|
| get | Key type key | Value_type | Returns the value to which the specified key is mapped, or null if the
map contains no value for this key. If the key is a string, the key value is case-sensitive. For example: Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); String code = colorCodes.get('Blue'); System.assertEquals(code, '0000A0'); // The following is not a color |
| getSObjectType | Schema.SObjectType | Returns the token of the sObject type that makes up the map
values. Use this with describe information, to determine if a map
contains sObjects of a particular type. For example:Account a = new Account( Name='Acme'); insert a; // Create a generic sObject Note that this method can only be used with maps that have sObject values. For more information, see Understanding Apex Describe Information. |
|
| isEmpty | Boolean | Returns true if the map has zero key-value pairs. For example:Map<String, String> colorCodes = new Map<String, String>(); Boolean empty = colorCodes.isEmpty(); system.assertEquals(empty, true); |
|
| keySet | Set of Key_type | Returns a set that contains all of the keys in the map. For
example:Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); Set <String> colorSet = new Set<String>(); colorSet = colorCodes.keySet(); |
|
| put | Key key, Value value |
Value_type | Associates the specified value with the
specified key in the map. 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. For example: Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'ff0000'); colorCodes.put('Red', '#FF0000'); // Red is now #FF0000 |
| putAll | Map m | Void | Copies all of the mappings from the specified map m to the original map. The new mappings from m replace any mappings that the original map had. |
| putAll | sObject[] l | If the map is of IDs or Strings to sObjects, adds the list of sObject records l to the map in the same way as the Map constructor with this input. | |
| remove | Key key | Value_type | Removes the mapping for this key from the
map if it is present. The value is returned by the method and then
removed. If the key is a string, the key value is case-sensitive. For example: Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); String myColor = colorCodes.remove('Blue'); String code2 = colorCodes.get('Blue'); System.assertEquals(code2, null); |
| size | Integer | Returns the number of key-value pairs in the map. For example: Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); Integer mSize = colorCodes.size(); system.assertEquals(mSize, 2); |
|
| values | list of Value_type | Returns a list that contains all of the values in the map in
arbitrary order. For example:Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); List<String> colors = new List<String>(); colors = colorCodes.values(); |
For more information on maps, see Maps.