如何将一个迭代器复制到另一个? [英] How to copy an iterator to another one?

查看:286
本文介绍了如何将一个迭代器复制到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于for循环的每次迭代,我都需要迭代一组值,但是仅对于第一次迭代,它可以正常工作.此后,itr.hasNext()返回false.

I need to iterate over the set of values for each iteration of for loop, but only for first iteration it works fine. Thereafter the itr.hasNext() returns false.

Iterator<String> itr = getQuestionIterator(File file);

for(Person p : persons)
{
    while(itr.hasNext())
    {
        String question = itr.next();
        ........
        ........
    }
}

这种行为对我很清楚.

一个解决方案可能是在for循环中调用getQuestionIterator(File file)方法,因此对于每个for循环迭代,它都会重新初始化.但这是非常低效的方法,因为itr是独立的.

One solution could be calling getQuestionIterator(File file) method in for loop so for each for loop iteration it gets reinitialized. But this is very inefficient approach as itr is independent.

我尝试了这个Iterator<String> temp = itr,但是它也只包含参考,所以也没有用.

I tried this Iterator<String> temp = itr , but it didn't work also as it holds the reference only.

有什么方法可以将迭代器复制到另一个或其他更好的方法中?

Is there any way to copy the iterator into another one or any other better approach?

推荐答案

Iterator是可以按顺序处理数据的最小的API,因此它从基础数据源中提取出来.由于它只能向前移动(next()),而没有任何重置或后退选项,因此它是一种单向对象,使用后必须将其丢弃.而且由于它提供的API有限,在不知道实现和/或底层数据源的情况下就不可能简单地复制"它.

An Iterator is the smallest possible API to work off data sequentially, hence it abstracts from the underlying data source. As it can only move forwards (next()) without any option to reset or rewind, it is a one-way object that must be thrown away after usage. And due to the limited API it offers, it is not possible to simply "copy" it without knowing the implementation and/or the underlying data source.

因此,有四种方法可以解决您的问题:

So there are four ways to handle your problem:

(1)从基础数据源重新获取新的迭代器

每次(再次)需要遍历数据时,只需调用getQuestionIterator(File file).

Just call getQuestionIterator(File file) every time you need to iterate over the data (again).

  • 优势:易于使用,易于实施.不需要缓存.
  • 缺点:性能(例如,必须再次读取/解析文件).同时,基础数据源可能已更改.

(2)将所有处理代码组合到一个迭代循环中

而不是...

while (iterator.hasNext()) { /* first processing step */ }
while (iterator.hasNext()) { /* second processing step */ }
while (iterator.hasNext()) { /* third processing step */ }
...

...组合所有步骤:

...combine all steps:

while (iterator.hasNext()) {
    String question = iterator.next();
    /* first processing step */
    /* second processing step */
    /* third processing step */
    ...
}

  • 优点:仅需要一个迭代器.不需要缓存.
  • 缺点:并非总是可能,例如处理步骤是否有依赖性.
  • (3)将所有元素复制到本地缓存(Collection)

    (3) Copy all elements into a local cache (Collection)

    对所有项目进行一次迭代,然后将它们放入本地集合中,您可以使用该集合来获取任意数量的迭代器:

    Iterate over all items once and put them into a local collection that you can use to aquire an arbitrary number of iterators:

    // read everything into a local cache
    Collection<String> cache = new ArrayList<>();
    while (iterator.hasNext()) cache.add(iterator.next());
    
    // now you can get as many iterators from cache as required:
    Iterator<String> iter = cache.iterator();
    // use iter
    
    iter = cache.iterator(); // once more
    // use iter
    ...
    

    • 优点:一旦所有数据都在缓存中,即可轻松实现,快速实施.
    • 缺点:需要额外的内存用于缓存.
    • (4)修改数据源API,以使其实现能够解决问题

      含义:更改getQuestionIterator(File file)以返回Iterable<String>而不是Iterator<String>.您可以从Iterable获得一个任意数量的迭代器:

      Meaning: Change getQuestionIterator(File file) to return an Iterable<String> instead of an Iterator<String>. You can gain an arbitrary number of iterators from an Iterable:

      Iterable<String> iterable = getQuestionIterator(File file);
      Iterator<String> iter = iterable.iterator();
      // use iter
      
      iter = iterable.iterator(); // once more
      // use iter
      

      • 优点:基础数据源最了解如何缓存数据.如果基础数据源已经使用缓存,则无需复制数据.
      • 缺点:并非总是可以更改API.
      • 这篇关于如何将一个迭代器复制到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆