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.
The following are constructors for Map.
public Map<T1,T2>()
Map<Integer, String> m1 = new Map<Integer, String>(); m1.put(1, 'First item'); m1.put(2, 'Second item');
public Map<T1,T2>(Map<T1,T2> mapToCopy)
Map<Integer, String> m1 = new Map<Integer, String>(); m1.put(1, 'First item'); m1.put(2, 'Second item'); Map<Integer, String> m2 = new Map<Integer, String>(m1); // The map elements of m2 are copied from m1 System.debug(m2);
public Map<ID,sObject>(List<sObject> recordList)
List<Account> ls = [select Id,Name from Account];
Map<Id, Account> m = new Map<Id, Account>(ls);
The following are methods for Map. All are instance methods.
public Void clear()
Type: Void
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.
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( 'San Francisco', map1.get(1).BillingCity); System.assertEquals( 'San Francisco', map2.get(1).BillingCity);
public Boolean containsKey(Object key)
Type: Boolean
If the key is a string, the key value is case-sensitive.
Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); Boolean contains = colorCodes.containsKey('Blue'); System.assertEquals(true, contains);
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.
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(); // Update the first entry of map1 map1.get(1).BillingCity = 'San Francisco'; // Verify that the BillingCity is updated in map1 but not in map2 System.assertEquals('San Francisco', map1.get(1).BillingCity); System.assertEquals('New York', map2.get(1).BillingCity);
public Boolean equals(Map map2)
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;.
public Object get(Object key)
Type: Object
If the key is a string, the key value is case-sensitive.
Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); String code = colorCodes.get('Blue'); System.assertEquals('0000A0', code); // The following is not a color in the map String code2 = colorCodes.get('Magenta'); System.assertEquals(null, code2);
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.
// Create a generic sObject variable. SObject sObj = Database.query('SELECT Id FROM Account LIMIT 1'); // Verify if that sObject variable is an Account token. System.assertEquals( Account.sObjectType, sObj.getSObjectType()); // Create a map of generic sObjects Map<Integer, Account> m = new Map<Integer, Account>(); // Verify if the map contains Account tokens. System.assertEquals( Account.sObjectType, m.getSObjectType());
public Integer hashCode()
Type: Integer
public Boolean isEmpty()
Type: Boolean
Map<String, String> colorCodes = new Map<String, String>(); Boolean empty = colorCodes.isEmpty(); System.assertEquals(true, empty);
public Set<Object> keySet()
Type: Set (of key type)
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();
public Object put(Object key, Object value)
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.
Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'ff0000'); colorCodes.put('Red', '#FF0000'); // Red is now #FF0000
public Void putAll(Map fromMap)
Type: Void
The new mappings from fromMap replace any mappings that the original map had.
Map<String, String> map1 = new Map<String, String>(); map1.put('Red','FF0000'); Map<String, String> map2 = new Map<String, String>(); map2.put('Blue','0000FF'); // Add map1 entries to map2 map2.putAll(map1); System.assertEquals(2, map2.size());
public Void putAll(sObject[] sobjectArray)
Type: Void
This method is similar to calling the Map constructor with the same input.
List<Account> accts = new List<Account>(); accts.add(new Account(Name='Account1')); accts.add(new Account(Name='Account2')); // Insert accounts so their IDs are populated. insert accts; Map<Id, Account> m = new Map<Id, Account>(); // Add all the records to the map. m.putAll(accts); System.assertEquals(2, m.size());
public Object remove(Key key)
Type: Object
If the key is a string, the key value is case-sensitive.
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(null, code2);
public Integer size()
Type: Integer
Map<String, String> colorCodes = new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); Integer mSize = colorCodes.size(); system.assertEquals(2, mSize);
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.
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();