程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何在Java中实现嵌套集合的迭代器?

发布于2023-12-25 22:23     阅读(760)     评论(0)     点赞(27)     收藏(5)


我有一个带有此表示的嵌套集合Collection<Collection<T>>我已经在类上实现了迭代器,但是 next() 方法没有给出正确的结果。它仅获取每个列表的第一个元素。示例List<List<String>>和值为{"1","2"},{"3","4"},{"5","6"}完整的班级布局。

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class NestedCollectionIterator implements Iterator<Object> {

private  Collection<? extends Collection<? extends Object>> _collOfColl = null;
private Iterator<? extends Collection<? extends Object>> itCollection = null;
private Iterator<? extends Object> innerIterator = null;    
Object next = null;

public NestedCollectionIterator( Collection<? extends Collection<? extends  Object>> collofColl){
    _collOfColl = collofColl;   
    itCollection = _collOfColl.iterator();
}

@Override
public boolean hasNext() {
    if(itCollection.hasNext()){
        innerIterator = itCollection.next().iterator();
        if(innerIterator != null || innerIterator.hasNext()){
            next = innerIterator.next();
            return true;
        }
    }
    return false;
}

public Object next() {
    if(hasNext()){
      Object obj = next;
     //Need some changes here.
       return obj;
    }
    return null;
}

@Override
public void remove() {}

}

测试实现的类

class Sample{
public static void main(String[] args){
    List<List<String>> Nestedlist = new ArrayList<List<String>>();
    List<String> l = new ArrayList<String>();
    l.add("1");
    l.add("2");
    Nestedlist.add(l);
    l = new ArrayList<String>();
    l.add("3");
    l.add("4");
    Nestedlist.add(l);
    l = new ArrayList<String>();
    l.add("5");
    l.add("6");
    Nestedlist.add(l);

    NestedCollectionIterator cc = new NestedCollectionIterator(Nestedlist);

    while(cc.hasNext()){
        System.out.println(cc.next.toString());
    }
  }
}

结果是1,3,5。如何使列表首先迭代列表中的所有元素,然后移动到其中的下一个集合项?

谢谢。


解决方案


这对我有用 - 它没有推广到Collection,但有一些实用方法可以为您提供最多三个级别的迭代器-迭代器Map我相信您可以将其适应一般集合。

public class NestedIterator<T> implements Iterator<T> {
  // Outer iterator. Goes null when exhausted.
  Iterator<Iterator<T>> i2 = null;
  // Inner iterator. Goes null when exhausted.
  Iterator<T> i1 = null;
  // Next value.
  T next = null;

  // Takes a depth-2 iterator.
  public NestedIterator(Iterator<Iterator<T>> i2) {
    this.i2 = i2;
    // Prime the pump.
    if (i2 != null && i2.hasNext()) {
      i1 = i2.next();
    }
  }

  @Override
  public boolean hasNext() {
    // Is there one waiting?
    if (next == null) {
      // No!
      // i1 will go null if it is exhausted.
      if (i1 == null) {
        // i1 is exhausted! Get a new one from i2.
        if (i2 != null && i2.hasNext()) {
          /// Get next.
          i1 = i2.next();
          // Set i2 null if exhausted.
          if (!i2.hasNext()) {
            // Exhausted.
            i2 = null;
          }
        } else {
          // Exhausted.
          i2 = null;
        }
      }
      // A null i1 now will mean all is over!
      if (i1 != null) {
        if (i1.hasNext()) {
          // get next.
          next = i1.next();
          // Set i1 null if exhausted.
          if (!i1.hasNext()) {
            // Exhausted.
            i1 = null;
          }
        } else {
          // Exhausted.
          i1 = null;
        }
      }
    }
    return next != null;
  }

  @Override
  public T next() {
    T n = next;
    next = null;
    return n;
  }

  @Override
  public void remove() {
    throw new UnsupportedOperationException("Not supported.");
  }

  // Iterating across Maps of Maps of Maps.
  static <K1, K2, K3, V> Iterator<Iterator<Iterator<V>>> iiiV(Map<K1, Map<K2, Map<K3, V>>> mapMapMap) {
    final Iterator<Map<K2, Map<K3, V>>> mmi = iV(mapMapMap);
    return new Iterator<Iterator<Iterator<V>>>() {
      @Override
      public boolean hasNext() {
        return mmi.hasNext();
      }

      @Override
      public Iterator<Iterator<V>> next() {
        return iiV(mmi.next());
      }

      @Override
      public void remove() {
        mmi.remove();
      }
    };
  }

  // Iterating across Maps of Maps.
  static <K1, K2, V> Iterator<Iterator<V>> iiV(Map<K1, Map<K2, V>> mapMap) {
    final Iterator<Map<K2, V>> mi = iV(mapMap);
    return new Iterator<Iterator<V>>() {
      @Override
      public boolean hasNext() {
        return mi.hasNext();
      }

      @Override
      public Iterator<V> next() {
        return iV(mi.next());
      }

      @Override
      public void remove() {
        mi.remove();
      }
    };
  }

  // Iterating across Map values.
  static <K, V> Iterator<V> iV(final Map<K, V> map) {
    return iV(map.entrySet().iterator());
  }

  // Iterating across Map.Entries.
  static <K, V> Iterator<V> iV(final Iterator<Map.Entry<K, V>> mei) {
    return new Iterator<V>() {
      @Override
      public boolean hasNext() {
        return mei.hasNext();
      }

      @Override
      public V next() {
        return mei.next().getValue();
      }

      @Override
      public void remove() {
        mei.remove();
      }
    };
  }

}


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/685769/60cb5270daa487abd37c/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

27 0
收藏该文
已收藏

评论内容:(最多支持255个字符)