How to check in Groovy that object is a list or collection or array?

前端 未结 6 804
情书的邮戳
情书的邮戳 2021-02-05 01:31

The question is as simple as the title. How to check in Groovy that object is a list or collection or array? But can\'t find a simple way of checking it. Any ideas?

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 01:56

    A List is a Collection, so the checks aren't mutually exclusive:

    def foo = ...
    boolean isCollection = foo instanceof Collection
    boolean isList = foo instanceof List
    boolean isSet = foo instanceof Set
    boolean isArray = foo != null && foo.getClass().isArray()
    

提交回复
热议问题