Last active
March 28, 2024 22:14
-
-
Save westc/1b689f6bd671f717817c9e490cc69881 to your computer and use it in GitHub Desktop.
Two implementations of zip() and zipKeys() in Apex (Salesforce).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Merges together the values of each of the arrays with the values at the | |
* corresponding position. Useful when you have separate data sources that are | |
* coordinated through matching array indexes. | |
*/ | |
public static Object[][] zip(Object[][] listOfArrays) { | |
Object[][] rows = new List<Object[]>(); | |
Integer itemsCount; | |
for (Object[] items : listOfArrays) { | |
Boolean isFirstPass = itemsCount == null; | |
if (isFirstPass) { | |
itemsCount = items.size(); | |
} | |
else if (itemsCount != items.size()) { | |
throw new ListException('All lists are not the same length.'); | |
} | |
for (Integer index = 0; index < itemsCount; index++) { | |
if (isFirstPass) { | |
rows.add(new Object[]{items[index]}); | |
} | |
else { | |
rows[index].add(items[index]); | |
} | |
} | |
} | |
return rows; | |
} | |
/** | |
* Takes a map where the keys are strings and values are lists. Returns a list | |
* of maps where the keys are the same keys of the given map but the value for | |
* each map corresponds to a value in the corresponding given list. | |
*/ | |
public static List<Map<String,Object>> zipKeys(Map<String,Object[]> mapOfLists) { | |
List<Map<String,Object>> maps = new List<Map<String,Object>>(); | |
Integer itemsCount; | |
for (String key : mapOfLists.keySet()) { | |
Object[] items = mapOfLists.get(key); | |
Boolean isFirstPass = itemsCount == null; | |
if (isFirstPass) { | |
itemsCount = items.size(); | |
} | |
else if (itemsCount != items.size()) { | |
throw new ListException('All lists are not the same length.'); | |
} | |
for (Integer index = 0; index < itemsCount; index++) { | |
if (isFirstPass) { | |
maps.add(new Map<String,Object>{key => items[index]}); | |
} | |
else { | |
maps[index].put(key, items[index]); | |
} | |
} | |
} | |
return maps; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run a single zip on an array of arrays. | |
System.debug(zip( | |
new Object[][]{ | |
new String[]{'John', 'Lana','Three','Four'}, | |
new String[]{'Gordon', 'Lang','Tres','Cuatro'}, | |
new Integer[]{1,2,3,4} | |
} | |
)); | |
// Run a double zip on an array of arrays proving that this essentially unzips | |
// an array of arrays. | |
System.debug(zip(zip( | |
new Object[][]{ | |
new String[]{'John', 'Lana','Three','Four'}, | |
new String[]{'Gordon', 'Lang','Tres','Cuatro'}, | |
new Integer[]{1,2,3,4} | |
} | |
))); | |
// Loop through each person. | |
List<Map<String,Object>> people = zipKeys(new Map<String,Object[]>{ | |
'firstName' => new String[]{'John', 'Lana'}, | |
'lastName' => new String[]{'Gordon', 'Lang'} | |
}); | |
for (Map<String,Object> person : people) { | |
System.debug('Hello ' + person.get('firstName') + ' ' + person.get('lastName') + '.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment