Do Apex collections implement iterable?

Do Apex collection classes implement Iterable interface? I want to write a generic function that can take any of the collection classes that support foreach. Is this possible?

Answer

The List<ANY> class implements Iterable<ANY>, but neither Map<ANY, ANY> nor Set<ANY> does.

Try the following code in v30:

System.debug((new set<id>()) instanceOf Iterable<Object>);
System.debug((new list<id>()) instanceOf Iterable<Object>);
System.debug((new map<id, object>()) instanceOf Iterable<Object>);
System.debug((new set<id>()) instanceOf Iterator<Object>);
System.debug((new list<id>()) instanceOf Iterator<Object>);
System.debug((new map<id, object>()) instanceOf Iterator<Object>);

Output:

12:30:33.073 (73681264)|USER_DEBUG|[1]|DEBUG|false
12:30:33.073 (73971363)|USER_DEBUG|[2]|DEBUG|true
12:30:33.074 (74136981)|USER_DEBUG|[3]|DEBUG|false
12:30:33.074 (74346453)|USER_DEBUG|[4]|DEBUG|false
12:30:33.074 (74526971)|USER_DEBUG|[5]|DEBUG|false
12:30:33.074 (74793803)|USER_DEBUG|[6]|DEBUG|false

So, in short, you need to handle each individually.

Attribution
Source : Link , Question Author : ipavlic , Answer Author : sfdcfox

Leave a Comment