No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Map Class
Namespace
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.
For more information on maps, see Maps.
Map Constructors
The following are constructors for Map.
Map<T1, T2>()
Signature
public Map<T1,T2>()
Example
1swfobject.registerObject("clippy.codeblock-0", "9");Map<Integer, String> m1 = new Map<Integer, String>();
2m1.put(1, 'First item');
3m1.put(2, 'Second item');
4Map<T1, T2>(Map<T1, T2>)
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
1swfobject.registerObject("clippy.codeblock-1", "9");Map<Integer, String> m1 = new Map<Integer, String>();
2m1.put(1, 'First item');
3m1.put(2, 'Second item');
4Map<Integer, String> m2 = new Map<Integer, String>(m1);
5// The map elements of m2 are copied from m1
6System.debug(m2);Map<ID, sObject>(List<sObject>)
Signature
public Map<ID,sObject>(List<sObject> recordList)
Parameters
- recordList
- Type: List<sObject>
- The list of sObjects to populate the map with.
Example
1swfobject.registerObject("clippy.codeblock-2", "9");List<Account> ls = [select Id,Name from Account];
2Map<Id, Account> m = new Map<Id, Account>(ls);Map Methods
The following are methods for Map. All are instance methods.
clear()
Signature
public Void clear()
Return Value
Type: Void
clone()
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
1Account a = new Account(
2 Name='Acme',
3 BillingCity='New York');
4
5Map<Integer, Account> map1 =
6 new Map<Integer, Account> {};
7map1.put(1, a);
8
9Map<Integer, Account> map2 =
10 map1.clone();
11map1.get(1).BillingCity =
12'San Francisco';
13
14System.assertEquals(
15 map1.get(1).BillingCity,
16 'San Francisco');
17
18System.assertEquals(
19 map2.get(1).BillingCity,
20 'San Francisco');containsKey(Object)
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
1Map<string, string> colorCodes =
2 new Map<String, String>();
3
4colorCodes.put('Red', 'FF0000');
5colorCodes.put('Blue', '0000A0');
6
7Boolean contains =
8 colorCodes.containsKey('Blue');
9System.assertEquals(contains, True);deepClone()
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
1Account a = new Account(
2 Name='Acme',
3 BillingCity='New York');
4
5Map<Integer, Account> map1 =
6 new Map<Integer, Account> {};
7
8map1.put(1, a);
9
10Map<Integer, Account> map2 =
11 map1.deepClone();
12
13map1.get(1).BillingCity =
14 'San Francisco';
15
16System.assertEquals(map1.get(1).
17 BillingCity, 'San Francisco');
18
19System.assertEquals(map2.get(1).
20 BillingCity, 'New York');equals(Map)
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)
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
1Map<String, String> colorCodes =
2 new Map<String, String>();
3
4colorCodes.put('Red', 'FF0000');
5colorCodes.put('Blue', '0000A0');
6
7String code =
8 colorCodes.get('Blue');
9
10System.assertEquals(code, '0000A0');
11
12// The following is not a color
13// in the map
14String code2 =
15 colorCodes.get('Magenta');
16
17System.assertEquals(code2, null);getSObjectType()
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
1Account a = new Account(
2 Name='Acme');
3insert a;
4
5// Create a generic sObject
6// variable s
7SObject s = Database.query
8 ('SELECT Id FROM Account ' +
9 'LIMIT 1');
10
11// Verify if that sObject
12// variable
13// is an Account token
14System.assertEquals(
15 s.getSObjectType(),
16 Account.sObjectType);
17
18// Create a map of generic
19// sObjects
20Map<Integer, Account> M =
21 new Map<Integer, Account>();
22
23// Verify if the list of sObjects
24// contains Account tokens
25System.assertEquals(
26 M.getSObjectType(),
27 Account.sObjectType);hashCode()
Signature
public Integer hashCode()
Return Value
Type: Integer
isEmpty()
Signature
public Boolean isEmpty()
Return Value
Type: Boolean
Example
1Map<String, String> colorCodes =
2 new Map<String, String>();
3Boolean empty = colorCodes.isEmpty();
4system.assertEquals(empty, true);keySet()
Signature
public Set<Object> keySet()
Return Value
Type: Set (of key type)
Example
1Map<String, String> colorCodes =
2 new Map<String, String>();
3
4colorCodes.put('Red', 'FF0000');
5colorCodes.put('Blue', '0000A0');
6
7Set <String> colorSet = new Set<String>();
8colorSet = colorCodes.keySet();put(Object, Object)
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
1Map<String, String> colorCodes =
2 new Map<String, String>();
3
4colorCodes.put('Red', 'ff0000');
5colorCodes.put('Red', '#FF0000');
6// Red is now #FF0000putAll(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[])
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)
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
1Map<String, String> colorCodes =
2 new Map<String, String>();
3
4colorCodes.put('Red', 'FF0000');
5colorCodes.put('Blue', '0000A0');
6
7String myColor = colorCodes.remove('Blue');
8String code2 =
9 colorCodes.get('Blue');
10
11System.assertEquals(code2, null);size()
Signature
public Integer size()
Return Value
Type: Integer
Example
1Map<String, String> colorCodes =
2 new Map<String, String>();
3
4colorCodes.put('Red', 'FF0000');
5colorCodes.put('Blue', '0000A0');
6
7Integer mSize = colorCodes.size();
8system.assertEquals(mSize, 2);values()
Signature
public List<Object> values()
Return Value
Type: List<Object>
Example
1Map<String, String> colorCodes =
2 new Map<String, String>();
3
4colorCodes.put('Red', 'FF0000');
5colorCodes.put('Blue', '0000A0');
6
7List<String> colors = new List<String>();
8colors = colorCodes.values();