public class MapToListAndSet {
// Method to convert Map values to a List
public static List<String> mapToList(Map<Integer, String> inputMap) {
List<String> resultList = new List<String>();
resultList.addAll(inputMap.values());
return resultList;
}
// Method to convert Map keys to a Set
public static Set<Integer> mapToSet(Map<Integer, String> inputMap) {
Set<Integer> resultSet = new Set<Integer>();
resultSet.addAll(inputMap.keySet());
return resultSet;
}
// Method to test the functionality of mapToList and mapToSet
public static void testMethods() {
// Create a sample map
Map<Integer, String> sampleMap = new Map<Integer, String>();
sampleMap.put(1, 'Apple');
sampleMap.put(2, 'Banana');
sampleMap.put(3, 'Cherry');
// Get List of values
List<String> resultList = mapToList(sampleMap);
System.debug('List of Map Values: ' + resultList);
// Get Set of keys
Set<Integer> resultSet = mapToSet(sampleMap);
System.debug('Set of Map Keys: ' + resultSet);
}
}
cHVibGljIGNsYXNzIE1hcFRvTGlzdEFuZFNldCB7CgogICAgLy8gTWV0aG9kIHRvIGNvbnZlcnQgTWFwIHZhbHVlcyB0byBhIExpc3QKICAgIHB1YmxpYyBzdGF0aWMgTGlzdDxTdHJpbmc+IG1hcFRvTGlzdChNYXA8SW50ZWdlciwgU3RyaW5nPiBpbnB1dE1hcCkgewogICAgICAgIExpc3Q8U3RyaW5nPiByZXN1bHRMaXN0ID0gbmV3IExpc3Q8U3RyaW5nPigpOwogICAgICAgIHJlc3VsdExpc3QuYWRkQWxsKGlucHV0TWFwLnZhbHVlcygpKTsKICAgICAgICByZXR1cm4gcmVzdWx0TGlzdDsKICAgIH0KCiAgICAvLyBNZXRob2QgdG8gY29udmVydCBNYXAga2V5cyB0byBhIFNldAogICAgcHVibGljIHN0YXRpYyBTZXQ8SW50ZWdlcj4gbWFwVG9TZXQoTWFwPEludGVnZXIsIFN0cmluZz4gaW5wdXRNYXApIHsKICAgICAgICBTZXQ8SW50ZWdlcj4gcmVzdWx0U2V0ID0gbmV3IFNldDxJbnRlZ2VyPigpOwogICAgICAgIHJlc3VsdFNldC5hZGRBbGwoaW5wdXRNYXAua2V5U2V0K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