6

This question already has an answer here:

I am trying to join String elements of Set with semicolon.

Set<String> elements = new Set<String>{
    'foo',
    'bar'
    };
System.debug(String.join(elements, ';'));

getting the following exception

Argument must implement Iterable

Is it weird, as we can easily iterate Set with foreach loop

for(String element :elements){
    System.debug(element);
}

It means, that Set<T> implements Iterable<T>, doesn't it? From documentation about String.join method:

Joins the elements of the specified iterable object, such as a List, into a single String separated by the specified separator. Signature

However, String.join with List collection works good.

System.debug(String.join(new List<String>(elements), ';'));

DEBUG|foo;bar

So do you have idea, why is it happening?

marked as duplicate by Oleksandr Berehovskyi, Community Apr 27 '18 at 6:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

Looks like Set does not implement iterable.

Code:

System.debug(new List<Object>() instanceof Iterable<Object>);

Output: Compile failure on line 2, column 14: Operation instanceof is always true since an instance of List<Object> is always an instance of Iterable<Object>


Code:

System.debug(new Set<Object>() instanceof Iterable<Object>);

Output: 11:38:23.10 (10761093)|USER_DEBUG|[2]|DEBUG|false

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