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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 16 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>();

setIdList.addAll(setIds);

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

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

share|improve this answer
9  
One-liner: Set<String> setIdSet = new Set<String>(String.join(new List<id>(setIds), ',').split(',')) – sfdcfox Jul 3 '14 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 Feb 14 at 23:14
1  
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. – E.J. Wilburn Mar 6 at 3:43

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 ) );
share|improve this answer
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 Mar 6 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 Mar 6 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. – E.J. Wilburn Mar 6 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. – E.J. Wilburn Mar 6 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 Mar 6 at 23:47

I tried a few things:

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

and

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

and

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

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

 
discard

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.