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

前端 未结 6 855
情书的邮戳
情书的邮戳 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 02:00

    I use this to "arrayfy" an object, if its already a collection then it will return a copy, else wrap it in a list. So you don't need to check it while processing, it will be always a collection.

    def arrayfy = {[] + it ?: [it]}
    def list = arrayfy(object) // will be always a list
    

提交回复
热议问题