Skip to main content

Posts

Showing posts with the label Remove value from a map

How to remove particular key from a map?

This is pretty simple one, One of my friend asked me how to remove a particular key-value pair from a map. Below is code snippet for the same. Hope It will help all. Map<String, String> colorCodes =   new Map<String, String>(); colorCodes.put('Red', 'FF0000'); colorCodes.put('Blue', '0000A0'); colorCodes.put('Blue2', '0000A0'); // If you know the key then just use like below      colorCodes.remove('Blue2'); System.debug('printing=== '+colorCodes); // if you are not sure what about the key or need some manipulation then follow below approach for(String key:colorCodes.keySet().clone()) {     // myMap.get(key), process that value     colorCodes.remove('Blue2'); } System.debug('printing==ater= '+colorCodes);