如何在 Groovy 中检查该对象是列表、集合还是数组?

发布于 2024-12-03 01:53:01 字数 67 浏览 0 评论 0原文

问题就如标题一样简单。如何在 Groovy 中检查该对象是列表、集合还是数组?但找不到简单的方法来检查它。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

下雨或天晴 2024-12-10 01:53:01

List 是一个 Collection,因此检查不是互斥的:

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

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()
半葬歌 2024-12-10 01:53:01

我不知道您是否需要区分 Collection、List 和 Array,或者只是想知道某个对象是否属于这些类型中的任何一种。如果是后者,您可以使用以下命令:

boolean isCollectionOrArray(object) {    
    [Collection, Object[]].any { it.isAssignableFrom(object.getClass()) }
}

// some tests
assert isCollectionOrArray([])
assert isCollectionOrArray([] as Set)
assert isCollectionOrArray([].toArray())
assert !isCollectionOrArray("str")

在 Groovy 控制台中运行上面的代码以确认其行为如广告所示

I don't know if you need to distinguish between Collection, List and Array, or just want to know if an object is any of these types. If the latter, you could use this:

boolean isCollectionOrArray(object) {    
    [Collection, Object[]].any { it.isAssignableFrom(object.getClass()) }
}

// some tests
assert isCollectionOrArray([])
assert isCollectionOrArray([] as Set)
assert isCollectionOrArray([].toArray())
assert !isCollectionOrArray("str")

Run the code above in the Groovy console to confirm it behaves as advertised

满地尘埃落定 2024-12-10 01:53:01

如果您正在寻找 Groovy 方式,请查看 in 运算符。它实际上是 Class.isAssignableFrom(Class)Class.isInstance(Object) 的组合,这意味着您可以使用它来测试类和对象。

// Test classes
assert ArrayList in Collection
assert ArrayList in List
assert HashSet in Collection
assert HashSet in Set

// Test objects
def list = [] as ArrayList
def set = [] as HashSet

assert list in Collection
assert list in List
assert set in Collection
assert set in Set

测试一个对象是否是数组可能很棘手。我会推荐@BurtBeckwith 的方法。

def array = [].toArray()

assert array.getClass().isArray()

If you are looking for a Groovy way, look at in operator. It is actually a combination of Class.isAssignableFrom(Class<?>) and Class.isInstance(Object) meaning that you can use it to test classes as well as objects.

// Test classes
assert ArrayList in Collection
assert ArrayList in List
assert HashSet in Collection
assert HashSet in Set

// Test objects
def list = [] as ArrayList
def set = [] as HashSet

assert list in Collection
assert list in List
assert set in Collection
assert set in Set

Testing if an object is an array may be tricky. I would recommend @BurtBeckwith's approach.

def array = [].toArray()

assert array.getClass().isArray()
苹果你个爱泡泡 2024-12-10 01:53:01

我用它来“arrayfy”一个对象,如果它已经是一个集合,那么它将返回一个副本,否则将其包装在一个列表中。所以处理时不需要检查它,它永远是一个集合。

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

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
只是我以为 2024-12-10 01:53:01

通常您需要使用鸭子打字来检查其行为。

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

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

def foo = someMethod()
if (foo.metaClass.respondsTo('each')) {
  foo.each {println it}
}
撧情箌佬 2024-12-10 01:53:01

只需使用instanceof运算符并检查该对象是否是java.util.Collection的实例

Just use the instanceof operator and check if the object is an instance of java.util.Collection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文