Mytutorialrack

Map Methods in Apex

In Apex, the Map class provides a collection of key-value pairs, where each key is unique. It offers several methods to manipulate and retrieve data from the map. Map methods in Apex provide a wide range of functionality, including searching for values, checking for key existence, and merging maps together. Let’s explore some of the commonly used methods with examples:

Map Methods in Apex

The following are methods for Map.

clear()

clear() method is used to remove all key-value pairs from a Map object, making it empty.

// Creating a map to store student names and their corresponding scores
Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);

// Printing the original map
System.debug('Original Map: ' + studentScores);

// Clearing the map using clear() method
studentScores.clear();

// Printing the cleared map
System.debug('Cleared Map: ' + studentScores);

clone()

In Apex, the clone() method is used to create a shallow copy of a Map object.

Creating a map to store employee details
Map<String, String> employeeDetails = new Map<String, String>();
employeeDetails.put('John', 'Manager');
employeeDetails.put('Emily', 'Developer');
employeeDetails.put('Michael', 'Analyst');

// Creating a shallow copy of the map using clone() method
Map<String, String> clonedMap = employeeDetails.clone();

// Modifying the cloned map
clonedMap.put('Emily', 'Senior Developer');
clonedMap.put('Michael', 'Senior Analyst');

// Printing the original map
System.debug('Original Map: ' + employeeDetails);

// Printing the cloned map
System.debug('Cloned Map: ' + clonedMap);

containsKey(key)

Checks if the map contains a specific key and returns a boolean value. Example:

Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);
Boolean containsJohn = studentScores.containsKey('John');
System.debug('Does Map contains John '+containsJohn);

deepClone()

In Apex, the deepClone() method is used to create a deep copy of a Map object, including both keys and values.

equals(map2)

In Apex, the equals(map2) method is used to compare two Map objects for equality. It returns a boolean value indicating whether the two maps have the same key-value pairs

	// Creating two maps to compare
Map<String, Integer> map1 = new Map<String, Integer>();
map1.put('John', 85);
map1.put('Emily', 92);
map1.put('Michael', 78);

Map<String, Integer> map2 = new Map<String, Integer>();
map2.put('John', 85);
map2.put('Emily', 92);
map2.put('Michael', 78);

Map<String, Integer> map3 = new Map<String, Integer>();
map3.put('John', 85);
map3.put('Emily', 92);
map3.put('Michael', 80);

// Comparing map1 with map2 using equals() method
Boolean isEqual1 = map1.equals(map2);
System.debug('Are map1 and map2 equal? ' + isEqual1); // Outputs: true

// Comparing map1 with map3 using equals() method
Boolean isEqual2 = map1.equals(map3);
System.debug('Are map1 and map3 equal? ' + isEqual2); // Outputs: false

get(key)

Retrieves the value associated with the specified key. If the key is not found, it returns null.

Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);
Integer johnScore = studentScores.get('John');
System.debug('JohnScore '+johnScore);

getSObjectType()

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

hashCode()

Returns the hashcode corresponding to this map.

isEmpty()

Checks if the map is empty and returns a boolean value. Example:

Map<String, Integer> studentScores = new Map<String, Integer>();
Boolean isMapEmpty = studentScores.isEmpty();
System.debug('before adding elements to map. isMapEmpty '+isMapEmpty); //returns true
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);
isMapEmpty = studentScores.isEmpty();
System.debug('After adding elements. isMapEmpty '+isMapEmpty); // returns false

keySet()

In Apex, the keySet() method of the Map class returns a Set containing all the keys present in the map. This allows you to access and iterate through the keys separately from the values. 

// Creating a map to store student names and their corresponding scores
Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);

// Retrieving the set of keys using keySet() method
Set<String> keys = studentScores.keySet();

// Iterating through the set of keys and printing each key and its associated score
for (String key : keys) {
    Integer score = studentScores.get(key);
    System.debug('Student: ' + key + ', Score: ' + score);
}

put(key, value)

This method adds a key-value pair to the map or updates the value if the key already exists. Example:

Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);

putAll(fromMap)

In Apex, the putAll(fromMap) method is used to add all key-value pairs from one Map object to another. It allows you to merge the contents of one map into another map. 

// Create two maps to demonstrate putAll() method
Map<String, Integer> map1 = new Map<String, Integer>();
map1.put('A', 1);
map1.put('B', 2);

Map<String, Integer> map2 = new Map<String, Integer>();
map2.put('C', 3);
map2.put('D', 4);

// Add all key-value pairs from map1 to map2 using putAll() method
map2.putAll(map1);

// Print the contents of map2
System.debug('Map2: ' + map2);

putAll(sobjectArray)

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

remove(key)

Removes the key-value pair associated with the specified key from the map. Example:

Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);
System.debug('Before Removal '+studentScores);
studentScores.remove('Michael');
System.debug('After removal '+studentScores);

size()

returns the number of key-value pairs in the map. Example:

Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);
Integer mapSize = studentScores.size();
System.debug('mapSize '+mapSize);

toString()

In Apex, the toString() method is used to convert a Map object into a String representation.

// Create a Map object
Map<String, Integer> mapt = new Map<String, Integer>();
mapt.put('A', 1);
mapt.put('B', 2);
mapt.put('C', 3);

// Convert the Map to a String using toString() method
String mapString = mapt.toString();

// Print the String representation of the Map
System.debug('Map String: ' + mapString);

values()

Returns a list of all the values in the map. Example:

Map<String, Integer> studentScores = new Map<String, Integer>();
studentScores.put('John', 85);
studentScores.put('Emily', 92);
studentScores.put('Michael', 78);
List<Integer> scores = studentScores.values();
System.debug('scores '+scores);
  1. Map methods in Apex provide powerful functionality for working with key-value pairs in Salesforce development.
  2. Developers can leverage various map methods to manipulate and analyze data stored in maps efficiently.
  3. Understanding the available map methods is essential for effectively managing and manipulating data structures in Apex.
  4. By utilizing map methods in Apex, developers can perform operations such as adding, removing, and iterating over key-value pairs in a map.
  5. Efficient utilization of map methods in Apex can enhance the performance and readability of code when dealing with collections of data.
Salesforce Development Training for Beginners

Share:

Recent Posts