是否有相当于 python 的 itertools.chain
的Java(有或没有第三方库)?
itertools.chain([1, 2, 3], [4, 5, 6]) # -> [1, 2, 3, 4, 5, 6]
像这样:
new Iterable<E> {
public Iterator<E> iterator() {
return new Iterator<E>() {
Iterator<E> i1 = list1.iterator();
Iterator<E> i2 = list2.iterator();
public boolean hasNext() {
return i1.hasNext() || i2.hasNext();
}
public E next() {
if(i1.hasNext()) {
return i1.next();
} else if(i2.hasNext()) {
return i2.next();
} else {
throw new NoSuchElementException("Lists exhausted");
}
}
public void remove() {
throw new UnsupportedOperationException("...");
}
}
}
}
最佳答案
Eclipse Collections在 LazyIterate
上有以下方法.
public static <T> LazyIterable<T> concatenate(Iterable<T>... iterables)
任何LazyIterable
也可以使用以下方法将自身与另一个可迭代对象连接起来。
LazyIterable<T> concatenate(Iterable<T> iterable)
注意:我是 Eclipse Collections 的提交者。
关于java - Python `itertools.chain` 等同于 Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13877281/