function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Identifying if object is of type Set or List at runtime



In Apex, at runtime we want to check if Object obj is of type Set or List where the type of elements in Set/List can be anything like String, Integer etc. 


obj (of type Set<String>) instanceOf Set<Object> returns false and we want to avoid having if statements for each possible type of set. 


Any pointers would be of great help.




There's no generic way to find out the type that I'm aware of, but you can use exception handling to help here.  Try to cast the object to a set of objects and if that fails, try to cast to a list.  E.g.


List<String> strs=new List<String>();

Object obj=strs;

Boolean isSet=false;
Boolean isList=false;

  Set<object> objSet=(Set<object>) obj;
catch (TypeException te)
  List<object> objList=(List<object>) obj;

System.debug('List? ' + isList + ', Set? ' + isSet);

produces the following output:


07:53:37.043 (43821499)|USER_DEBUG|[19]|DEBUG|List? true, Set? false




Please elaborate your problem statement, maybe post a snippet of code, that would give us a better idea of the exact issue.

Thanks for the quick reply.


However, it doesn't work when it is Set. We can't type cast Set<String> to String<Object>, so even though it is set we won't be able to figure it out.


Please correct me if I am missing something.




Bob is right, there is no straight way that I know of either. But yes you can write code in various different ways to handle it.

Whats the original problem? If you don't mind my asking. I would say this is a more or programmatic issue.

Yes Auyon. It is a programmatic issue. However, I am not able to figure out any way to get this done.


My problem is, depending on the type of Object I want to do some processing. The type can be set and list also, of which element type can be anything again.


For other types like simple String, Integer.. I could use instanceOf operator, but for list and set not able to find any way.


I hope the problem is clear now.

I understand.

Well, internally Apex writes java servlets and therefore very much aligned to Java's rules. So any relevant solution you find in Java should ethically work in Apex as well.

But, since we are trying to get our hands dirty...So just to understand do we need to know whether the object is a LIST or a SET or also what it is a LIST or SET Of? Because, as in Java although you cannot do a instanceOf in List or Set but the elements which they carry you can.

Appreciate if you can share some snippets, we can treat that as a starting point.

Sorry, I won't be able to share the code snippet.


Knowing the object is a List or Set would suffice here.



Well in that case try doing some searches in Java and you can pull some ideas from there.

I will post a solution if I find one

You could try to cast to a set of objects and then inspect the TypeException error message to see if it refers to a different type of set, in which case you know it is a set.  Its a bit fragile, in that if SFDC change the exception message it would stop working.


The only other thing I can think of is to use the String.valueOf() method and inspect the output.  A set will be comma-separated values bound by braces, while a list is comma-separated values bound by brackets.  Still fragile though, as the object could be a string that is formatted that way, so its likely to be some combination of use String.valueOf then try to cast it and see if you get an error.


There is no way to inspect an instance though - I've done some digging and the advice every time is to build a long list of if statements.


Thanks Bob.