簡體   English   中英

如何在Java中的嵌套集合上實現迭代器?

[英]How to implement iterator on nested collection in Java?

我有一個具有此表示形式Collection<Collection<T>>的嵌套集合。 我已經在類上實現了Iterator,但是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但是有實用程序方法可以為您提供多達3個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();
      }
    };
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM