Map Class

Contains methods for the Map collection type.

Namespace

System

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.

Note
  • 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 equals and hashCode methods, 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.
  • 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, and remove treat these keys as distinct.

For more information on maps, see Maps.

Map Constructors

The following are constructors for Map.

Map<T1, T2>()

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.

Signature

public Map<T1,T2>()

Example

Map<Integer, String> m1 = new Map<Integer, String>();
m1.put(1, 'First item');
m1.put(2, 'Second item');

Map<T1, T2>(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.

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<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);

Map<ID, sObject>(List<sObject>)

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.

Signature

public Map<ID,sObject>(List<sObject> recordList)

Parameters

recordList
Type: List<sObject>
The list of sObjects to populate the map with.

Example

List<Account> ls = [select Id,Name from Account];
Map<Id, Account> m = new Map<Id, Account>(ls);

Map Methods

The following are methods for Map. All are instance methods.

clear()

Removes all of the key-value mappings from the map.

Signature

public Void clear()

Return Value

Type: Void

clone()

Makes a duplicate copy of the map.

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

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');

containsKey(Object)

Returns true if the map contains a mapping for the specified 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

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()

Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.

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

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');

equals(Map)

Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false.

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(Object)

Returns the value to which the specified key is mapped, or null if the map contains no value for this 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

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()

Returns the token of the sObject type that makes up the map values.

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

Account a = new Account(
   Name='Acme');
insert a;

// Create a generic sObject 
// variable s SObject s = Database.query ('SELECT Id FROM Account ' + 'LIMIT 1'); // Verify if that sObject
// variable
// is an Account token System.assertEquals( s.getSObjectType(), Account.sObjectType); // Create a map of generic
// sObjects Map<Integer, Account> M = new Map<Integer, Account>(); // Verify if the list of sObjects
// contains Account tokens System.assertEquals( M.getSObjectType(), Account.sObjectType);

hashCode()

Returns the hashcode corresponding to this map.

Signature

public Integer hashCode()

Return Value

Type: Integer

isEmpty()

Returns true if the map has zero key-value pairs.

Signature

public Boolean isEmpty()

Return Value

Type: Boolean

Example

Map<String, String> colorCodes = 
   new Map<String, String>();
Boolean empty = colorCodes.isEmpty();
system.assertEquals(empty, true);

keySet()

Returns a set that contains all of the keys in the map.

Signature

public Set<Object> keySet()

Return Value

Type: Set (of key type)

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(Object, Object)

Associates the specified value with the specified key in the map.

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

Map<String, String> colorCodes = 
   new Map<String, String>();

colorCodes.put('Red', 'ff0000');
colorCodes.put('Red', '#FF0000');
// Red is now #FF0000

putAll(Map)

Copies all of the mappings from the specified map to the original map.

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.

putAll(sObject[])

Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.

Signature

public putAll(sObject[] sobjectArray)

Parameters

sobjectArray
Type: sObject[]

Return Value

Type:

Usage

This method is similar to calling the Map constructor with the same input.

remove(Key)

Removes the mapping for the specified key from the map, if present, and returns the corresponding value.

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

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()

Returns the number of key-value pairs in the map.

Signature

public Integer size()

Return Value

Type: Integer

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()

Returns a list that contains all of the values in the map in arbitrary order.

Signature

public List<Object> values()

Return Value

Type: List<Object>

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();
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.