Apex Code
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black'};
System.debug(Colors);
keySet()
returns a set that contains all of the keys in the map.
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black', '#ff0000'=>'Red'};
System.debug(COlors.keySet());
values()
returns a list that contains all the values in the map.
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black', '#ff0000'=>'Red'};
for (String i : Colors.value()) {
System.debug(i);
}
The keyword put()
can be used to add more items in the map:
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black'};
Colors.put('#ff0000','Red');
Colors.put('#00ff00','Green');
Colors.put('#0000ff','Blue');
System.debug(Colors);
Note: The keyword put() does not insert the new items at the end of the map.
To remove all the items in the Map, use the clear()
method:
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black', '#ff0000'=>'Red'};
Colors.clear();
System.debug(Colors);
To remove an item of the map, use the remove()
method:
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black', '#ff0000'=>'Red'};
Colors.remove('#00000');
System.debug(Colors);
You can use the size()
method to find the number of items of the map:
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black', '#ff0000'=>'Red'};
System.debug(Colors.size());
Use for-each
loop to list all the items of the map:
Map<string, string> Colors = new Map<string, string>
{'#FFFFFF'=>'White', '#00000'=>'Black', '#ff0000'=>'Red'};
for (String i : Colors.keySet()) {
System.debug(i);
}
Map<Id, Account> acMaP = new Map<Id, Account>();
List<Account> acList =[SELECT Id, Name from Account];
for (Account ac : acList){
acMap.put(ac.Id, ac);
}
System.debug(acMaP.keySet());
System.debug(acMaP.values());
A new defined map can have an existing map:
Map<Integer, String> mapA = new Map<Integer, String>();
mapA.put(1, 'A');
mapA.put(2, 'B');
Map<Integer, String> mapB = new Map<Integer, String>(mapA);
System.debug(mapB);
System.debug(mapA.equals(mapB));
USER_DEBUG [5]|DEBUG|{1=A, 2=B}
USER_DEBUG [7]|DEBUG|true
Use the method clone()
to meake a duplicate copy of the map:
Map<Integer, String> mapA = new Map<Integer, String>();
mapA.put(1, 'A');
mapA.put(2, 'B');
Map<Integer, String> mapB = mapA.clone();
System.debug(mapB);
System.debug(mapA.equals(mapB));
USER_DEBUG [5]|DEBUG|{1=A, 2=B}
USER_DEBUG [7]|DEBUG|true
Use the method containsKey(key)
to return true if the map contains a mapping for the specified key.
Map<Integer, String> mapA = new Map<Integer, String>();
mapA.put(1, 'A');
mapA.put(2, 'B');
System.debug(mapA.containskey(2));
System.debug(mapA.containskey(5));
USER_DEBUG [5]|DEBUG|true
USER_DEBUG [6]|DEBUG|false
Use the method deepClone()
to make a duplicate copy of a map, including sObject records if this is a map with sObject record values.
Use the method equals()
to compare a map with the specified map and returns true if both maps are equal; otherwise, returns false.
Map<Integer, String> mapA = new Map<Integer, String>();
mapA.put(1, 'A');
mapA.put(2, 'B');
Map<Integer, String> mapB = new Map<Integer, String>();
mapB.put(1, 'A');
mapB.put(2, 'B');
System.debug(mapA.equals(mapB));
USER_DEBUG [9]|DEBUG|true
Use the method get(key)
to return the value to which the specified key is mapped, or null if the map contains no value for this key.
Map<Integer, String> mapA = new Map<Integer, String>();
mapA.put(1, 'A');
mapA.put(2, 'B');
System.debug(mapA.get(2));
USER_DEBUG [5]|DEBUG|B
Use the method getSObjectType()
to return the token of the sObject type that makes up the map values.
Use the method hashCode()
return the hashcode corresponding to this map.
Use the method isEmpty()
return true if the map has zero key-value pairs.
Map<Integer, String> mapA = new Map<Integer, String>();
mapA.put(1, 'A');
mapA.put(2, 'B');
mapA.clear();
System.debug(mapA.isempty());
USER_DEBUG [7]|DEBUG|true
Use the method putAll(fromMap)
to copy all of the mappings from the specified map to the original map.
Use the method toString()
to return the string representation of the map.
public class mapExample1 {
public static Map<Integer,Integer> mapMethod(Integer n){
//n is the size of the map:
Map<Integer, Integer> mapA = new Map<Integer, Integer>();
for(Integer i=1; i<=n; i++ ){
mapA.put(i,i*i); //1st Loop: (1==>1) 2nd Loop: (2==>4)
}
for(Integer a : mapA.keyset()){
System.debug(a + '=>' + mapA.get(a));
}
return mapA;
}
}