Collections Groovy findIndexValues返回列表<;长期>;

Collections Groovy findIndexValues返回列表<;长期>;,collections,types,groovy,Collections,Types,Groovy,我一定是疯了,但为什么Groovy findIndexValues返回List?我可以得到整数的索引吗 foo = ['a','b','d','e', 'e','e'] indices = foo.findIndexValues { it == 'e'} indices.each { println foo[it] } 上述操作将崩溃,因为foo集合无法处理长索引。我使用的语言不是应该的吗?这就是该方法的工作原理。它使用迭代器遍历集合,并作为long跟踪匹配的索引。理论上,它支持大于Inte

我一定是疯了,但为什么Groovy findIndexValues返回
List
?我可以得到整数的索引吗

foo = ['a','b','d','e', 'e','e']
indices  = foo.findIndexValues { it == 'e'}
indices.each { println foo[it] }

上述操作将崩溃,因为foo集合无法处理长索引。我使用的语言不是应该的吗?

这就是该方法的工作原理。它使用迭代器遍历集合,并作为long跟踪匹配的索引。理论上,它支持大于
Integer.MAX_VALUE
的集合,尽管我怀疑这在实践中是否有用

您可以通过以下方式解决此问题:

indices.each { println foo[it as int] }

与每次使用索引时都进行转换不同,只需在初始赋值中预先转换所有索引即可

foo = ['a','b','d','e', 'e','e']
indices  = foo.findIndexValues { it == 'e'}.collect { it as Integer }
indices.each { println foo[it] }

我觉得这是一个糟糕的实施决定。尤其是当文档中没有这样说明时。我想我只能接受它了。findIndexValues在一个iterable上工作,所以可以有比列表更多的元素