About duplicate
This is NOT a duplicate of How to clone an iterator?
Please do not blindly close this question, all the answers given in so-called duplicate DO NOT work. The OP is in charge of the other problem, and obviously, the answers fitted HIS problem, but not mine.
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.
Problem
I have iterator. I would like to get copy (duplicate) of it, so then I could proceed with original and copy completely independently.
Important
Copying through reflection or serialization is no-go (performance penalty).
Example
var list = List(1,2,3,4,5)
var it1 = list.iterator
it1.next()
var it2 = it1 // (*)
it2.next()
println(it1.next())
This would make simply reference to it1, so when changing it1, it2 changes as well and vice-versa.
The example above uses List, I am currently struggling with HashMap, but the question is general one -- just iterator.
Approach #1
If you edit line (*) and write:
var it2 = it1.toList.iterator
(this was suggested as solution in the linked question) the exception is thrown while executing the program.
Approach #2
"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.
duplicate
.) – Rex Kerr