【发布时间】:2011-11-21 18:13:27
【问题描述】:
有没有办法使用 groovy .collect 方法,但只能在源数组中达到某个索引?
例如,如果您的源迭代器长度为 100 万,而您的限制为 100,那么您最终会得到一个包含 100 个项目的数组。
【问题讨论】:
有没有办法使用 groovy .collect 方法,但只能在源数组中达到某个索引?
例如,如果您的源迭代器长度为 100 万,而您的限制为 100,那么您最终会得到一个包含 100 个项目的数组。
【问题讨论】:
从 Groovy 1.8.1 开始你也可以这样做
list.take(100).collect { ... }
其中 take 将返回列表中的前 100 个元素。
【讨论】:
如果您正在使用任何实现java.util.List 的数据结构,您可以对其执行collection.subList(0, 100)。其中 0 是开始索引,100 是结束。之后,您会将新集合传递给collect()。
这是一个使用扩展 java.util.Iterator 的对象的示例:
public class LimitIterator implements Iterator, Iterable {
private it
private limit
private count
LimitIterator(Iterator it, int limit) {
limit = limit;
count = 0;
it = it
}
boolean hasNext(){
return (count >= limit) ? false : it.hasNext()
}
Object next() {
if (!hasNext()) throw new java.util.NoSuchElementException()
count++
return it.next()
}
Iterator iterator(){
return this;
}
void remove(){
throw new UnsupportedOperationException("remove() not supported")
}
}
// Create a range from 1 to 10000
// and an empty list.
def list = 1..10000
def shortList = []
// Ensure that everything is as expected
assert list instanceof java.util.List
assert list.iterator() instanceof java.util.Iterator
assert list.size() == 10000
assert shortList instanceof java.util.List
// Grab the first 100 elements out of the lists iterator object.
for (i in new LimitIterator(list.iterator(), 100)) {
shortlist.add(i);
}
assert shortlist.size() == 100
【讨论】:
toList() 或as List 将其转换为列表
您可以使用一系列索引来获取子列表,然后将collect 应用于子列表。
def result = list[0..100].collect { ... }
【讨论】: