【问题标题】:Python `itertools.chain` equivalent in Java?Java 中的 Python `itertools.chain` 等价物?
【发布时间】:2012-12-14 10:55:33
【问题描述】:

是否有相当于 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("...");
               }
           }
       }
   }

【问题讨论】:

    标签: java python collections iteration combinations


    【解决方案1】:

    Eclipse CollectionsLazyIterate 上有如下方法。

    public static <T> LazyIterable<T> concatenate(Iterable<T>... iterables)
    

    任何LazyIterable 都可以使用以下方法将自己与另一个可迭代对象连接起来。

    LazyIterable<T> concatenate(Iterable<T> iterable)
    

    注意:我是 Eclipse Collections 的提交者。

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 2019-11-03
      • 2011-12-15
      • 2015-02-23
      • 2011-04-13
      相关资源
      最近更新 更多