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?


4 Answers 4


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);

You can explicitly cast from List<Id> to List<String> and vice-versa and use that to convert Sets between types by converting the Set to a List first and passing the List a new Set<>( List<> ) call.

Here's the simplest single-line method to convert from a Set of Ids to a Set of Strings, where idSet is defined as Set<Id> idSet:

Set<String> stringSet = new Set<String>( (List<String>)new List<Id>( idSet ) );

Here are some more examples of converting Sets and Arrays between Ids to String types:

// Convert List<Id> to List<String> - Id[] and String[] array notation works as well.
List<Id> ids = new List<Id>{ '001800000000001AAA', '001800000000002AAA' };
List<String> strings = new List<String>( (List<String>)ids );

// Convert Set<Id> to Set<String>
Set<Id> idSet = new Set<Id>{ '001800000000001AAA', '001800000000002AAA' };
Set<String> stringSet = new Set<String>( (List<String>)new List<Id>( idSet ) );

// Convert List<String> to List<Id>
List<String> stringList = new List<String>{ '001800000000001AAA', '001800000000002AAA' };
List<Id> idList = new List<Id>( (List<Id>)stringList );

// Convert Set<String> to Set<Id>
Set<String> stringSet2 = new Set<String>{ '001800000000001AAA', '001800000000002AAA' };
Set<Id> idSet2 = new Set<Id>( (List<Id>)new List<String>( stringSet2 ) );
  • 1
    This is what I've used in the past. It's tough to dig out the lines that actually correspond to OP, can you feature them more prominently in your post?
    – Adrian Larson
    Commented Mar 6, 2016 at 14:59
  • That's interesting, actually. I don't believe you used to be able to cast collection of Id to a collection of String before (back when this question was first asked). I wonder if that's relatively new?
    – sfdcfox
    Commented Mar 6, 2016 at 18:15
  • I'm pretty sure this didn't work in the past as I know I tried to solve this problem years ago and then switched over to just looping through the list. That was before the String.join() and String.split() option was available as well. Who knows when this started working. @AdrianLarson - Modified the answer as you requested. Commented Mar 6, 2016 at 23:36
  • Also, on an interesting side note, if you just try and cast a Set<Id> to Set<String> and vice-versa you get a lovely instance of Set is never an instance of Set error message, which seems like a bug to me. Commented Mar 6, 2016 at 23:37
  • I'm fairly certain it's worked for at least a year and a half, but I didn't try it before that.
    – Adrian Larson
    Commented Mar 6, 2016 at 23:47

I couldn't find a direct one line conversion but below is a way to convert a set of ids to a set of strings without using a for loop. I'm assuming you wanted a one liner but here you go anyway!

Set<Id> setIds = new Set<Id>();

List<Id> setIdList = new List<Id>();


String Ids = String.join(setIdList, ',');

Set<String> setIdSet = new Set<String>(Ids.split(','));

  • 14
    One-liner: Set<String> setIdSet = new Set<String>(String.join(new List<id>(setIds), ',').split(','))
    – sfdcfox
    Commented Jul 3, 2014 at 15:54
  • Jenny B and @sfdcfox - if setIds is a set of 0 elms, this returns setIdSet as a set of strings of size 1 with the 0th element = string of len 0; expected result would be a set of size 0 - found while I was unit testing a util method to do this little snippet. split on an empty string yields an array of size 0 with value of empty string. stackoverflow.com/questions/4964484/…
    – cropredy
    Commented Feb 14, 2016 at 23:14
  • 3
    I'd like to point out that this method is about 5-10x's faster than the loop method for large lists/sets and around 30 times faster than the JSON serialize/deseralize method accepted as the answer. As sfdcfox pointed out, it can be done as a single line as well. Benchmarking was done on Spring '16. Commented Mar 6, 2016 at 3:43

I tried a few things:

Set<Id> ids = new Set<Id>();
Set<String> other = (Set<String>)ids;


Set<Id> ids = new Set<Id>();
Set<String> other = new Set<String>{ids};


Set<Id> ids = new Set<Id>();
Set<String> other = new Set<String>();

None of those seemed to work and I did a quick google search and don't see anything else that works either. It appears you are stuck with a loop. The only case I can see this being an issue, is if you have a Set<Id> somewhere already and need to add strings to it. Best way to fix this, is to just make the original Set a Set<String>.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .