Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to copy iterator in Scala?
    primarykey
    data
    text
    <h2>About duplicate</h2> <p><em>This is NOT a duplicate of <a href="https://stackoverflow.com/questions/5220108/a-very-simple-question-how-to-clone-an-iterator">How to clone an iterator?</a></em></p> <p>Please do not blindly close this question, all the answers given in so-called duplicate <strong>DO NOT</strong> work. The OP is in charge of the other problem, and obviously, the answers fitted HIS problem, but not mine.</p> <p>Not every similar question is a duplicate, there is such feature as "expansion question" on SE, the only way is to ask again on the same subject to get different, working, answers.</p> <h2>Problem</h2> <p>I have iterator. I would like to get copy (duplicate) of it, so then I could proceed with original and copy completely independently.</p> <p><strong>Important</strong></p> <p>Copying through reflection or serialization is no-go (performance penalty).</p> <p><strong>Example</strong></p> <pre><code>var list = List(1,2,3,4,5) var it1 = list.iterator it1.next() var it2 = it1 // (*) it2.next() println(it1.next()) </code></pre> <p>This would make simply reference to <strong>it1</strong>, so when changing <strong>it1</strong>, <strong>it2</strong> changes as well and vice-versa.</p> <p><em>The example above uses <strong>List</strong>, I am currently struggling with <strong>HashMap</strong>, but the question is general one -- just iterator.</em></p> <h3>Approach #1</h3> <p>If you edit line (*) and write:</p> <pre><code>var it2 = it1.toList.iterator </code></pre> <p>(this was suggested as solution in the linked question) the exception is thrown while executing the program.</p> <h3>Approach #2</h3> <p>"You take the list and...". No, I don't. I don't have a list, I have iterator. In general I don't know anything about collection which underlies the iterator, the only thing I have is iterator. I have to "fork" it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload