7

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?

2

1 Answer 1

5

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

2

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