Skip to content

UTIL_Map

Class · Group: Utilities

apex
global inherited sharing class UTIL_Map

Static helper methods for common Map operations in Apex, including key-value transformation, entry joining, equality comparison, SObject indexing, and case-insensitive lookups.

Since: 1.0

Example:

apex
Map<String, String> params = new Map<String, String>{'key' => 'value', 'name' => 'test'};
String delimited = UTIL_Map.toDelimitedString(params, ',');

Methods

MethodDescription
global static List flattenValues(Map<Id, List<SObject>> idMap)Collapses an Id-keyed map of SObject lists into a single flat list containing every record.
global static List flattenValues(Map<String, List<SObject>> stringMap)Collapses a String-keyed map of SObject lists into a single flat list containing every record.
global static String toDelimitedString(Map<Map separator)Serialises a map of name-value pairs into a single delimited string of the form name=value[separator]name=value.

Inner Classes

ClassDescription
CaseInsensitiveMapA Map implementation that performs case-insensitive key lookups.

Method Details

flattenValues

apex
global static List<SObject> flattenValues(Map<Id, List<SObject>> idMap)

Collapses an Id-keyed map of SObject lists into a single flat list containing every record.

Parameters:

  • idMap (Map) - A map where the value is a list of SObjects and the key is an Id

Returns: SObject - A list containing all the SObjects from the map

Since: 1.0

Example:

apex
Map<Id, List<SObject>> contactsByAccount = new Map<Id, List<SObject>>();
List<SObject> allContacts = UTIL_Map.flattenValues(contactsByAccount);

flattenValues

apex
global static List<SObject> flattenValues(Map<String, List<SObject>> stringMap)

Collapses a String-keyed map of SObject lists into a single flat list containing every record.

Parameters:

  • stringMap (Map) - A map where the value is a list of SObjects and the key is a string

Returns: SObject - A list containing all the SObjects from the map

Since: 1.0

Example:

apex
Map<String, List<SObject>> recordsByType = new Map<String, List<SObject>>();
List<SObject> allRecords = UTIL_Map.flattenValues(recordsByType);

toDelimitedString

apex
global static String toDelimitedString(Map<String, String> valuesByNameMap, String separator)

Serialises a map of name-value pairs into a single delimited string of the form name=value[separator]name=value.

Parameters:

  • valuesByNameMap (Map) - A map of name and values
  • separator (String) - The separator to use between pairs; defaults to comma if null

Returns: String - A string in the format paramName=paramValue[separator]paramName2=paramValue2

Since: 1.0

Example:

apex
Map<String, String> params = new Map<String, String>{ 'key1' => 'value1', 'key2' => 'value2' };
String result = UTIL_Map.toDelimitedString(params, ',');