Sign up ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a very simple question. I have Set of Ids which I want to convert to Set of String. I dont want to use for loop to iterate over it. What will be the simplest way to do the same?

share|improve this question

3 Answers 3

up vote 12 down vote accepted

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);
share|improve this answer

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(','));

share|improve this answer
One-liner: Set<String> setIdSet = new Set<String>(String.join(new List<id>(setIds), ',').split(',')) – sfdcfox Jul 3 '14 at 15:54

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>.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.