Map Methods

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.

Note
In the table below:
  • Key_type represents the primitive type of a map key.
  • Value_type represents the primitive or sObject type of a map value.
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:

Invoice_Statement__c a = new 
   Invoice_Statement__c(
     Description__c='Invoice1');

Map<Integer, 
  Invoice_Statement__c> map1 = 
    new Map<Integer, Invoice_Statement__c> {};
map1.put(1, a);

Map<Integer, 
   Invoice_Statement__c> map2 = 
     map1.clone();
map1.get(1).Description__c = 
   'New invoice';

System.assertEquals(
   map1.get(1).Description__c,
   'New invoice');

System.assertEquals(
   map2.get(1).Description__c,
   'New invoice');

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 case of the String value matters.

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:
Invoice_Statement__c a = new 
   Invoice_Statement__c(
     Description__c='Invoice1');

Map<Integer, 
   Invoice_Statement__c> map1 = 
      new Map<Integer, 
            Invoice_Statement__c> {};

map1.put(1, a);

Map<Integer, 
   Invoice_Statement__c> map2 = 
      map1.deepClone();

map1.get(1).Description__c = 
   'New invoice';

System.assertEquals(
   map1.get(1).
   Description__c, 'New invoice');

System.assertEquals(
   map2.get(1).
   Description__c, 'Invoice1');

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. 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  
    
// in the map  
    
String code2 = 
   colorCodes.get('Magenta');

System.assertEquals(code2, null);
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:
Invoice_Statement__c a = new 
   Invoice_Statement__c(
      Description__c='Invoice1');
insert a;

// Create a generic sObject  
    
// variable s 
    
SObject s = Database.query
   ('SELECT Id FROM ' + 
    'Invoice_Statement__c ' +
    'LIMIT 1');

// Verify if that sObject  
    
// variable is an  
    
// Invoice_Statement__c token 
    
System.assertEquals(
   s.getSObjectType(), 
   Invoice_Statement__c.sObjectType);

// Create a map of generic  
    
// sObjects  
    
Map<Integer, 
   Invoice_Statement__c> M = 
     new Map<Integer, 
       Invoice_Statement__c>();

// Verify if the list of  
    
// sObjects contains  
    
// Invoice_Statement__c tokens 
    
System.assertEquals(
   M.getSObjectType(), 
   Invoice_Statement__c.sObjectType);

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. 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. 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.

© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.