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

前端 未结 6 864
情书的邮戳
情书的邮戳 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:44

    Usually you'd want to check its behavior with duck typing.

    def foo = someMethod()
    if (foo.metaClass.respondsTo('each')) {
      foo.each {println it}
    }
    

提交回复
热议问题