[SalesForce] Convert a Set to Set using Apex

I have a Set<Id> which I want to convert to a Set<String>. I don't want to use a for loop to iterate over it. What is the simplest way to do this?

Best Answer

Here's a clever (ugly) one-liner.

Set<Id> ids = new Set<Id>{'001C000001DgWjE','001C000001DgWjD'};

// Here's the one line!
Set<String> idStrs = (Set<String>)JSON.deserialize(JSON.serialize(ids), Set<String>.class);

System.debug('idStrings=' + idStrs);
Related Topic