function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sales Person 416Sales Person 416 

how to remove element from map

how to  remove element from map
jigarshahjigarshah
Use the following method to remove elements from the instance of a map - mapinstance.remove(your_map_key).

The sample code snippet below showcases how to remove an element from a Map within Apex.
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);

Refer to the following documentation link to understand further. - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

Hope this helps.

Please do not forget to mark this thread as SOLVED and the answer as the BEST ANSWER if it helps you resolve your issue.
AKumAKum

Hi @Sales Person 416,

Map contains values as per keys . So every key can be removed so that its related value would also be deleted . In your case you should use remove method of maps.