Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6698503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:35:08+00:00 2026-05-26T06:35:08+00:00

About duplicate This is NOT a duplicate of How to clone an iterator? Please

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:35:09+00:00Added an answer on May 26, 2026 at 6:35 am

    You can’t duplicate an iterator without destroying it. The contract for iterator is that it can only be traversed once.

    The question you linked to shows how to get two copies in exchange for the one you’ve destroyed. You cannot keep using the original, but you can now run the two new copies forward independently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is about removing sequences from an array, not duplicates in the strict
Earlier this week I ask a question about filtering out duplicate values in sequence
Please note this is not duplicate of File r/w locking and unlink . (The
Before this question is marked as duplicate, please read it. ;) There are already
Duplicate What are your hard rules about commenting? A Developer I work with had
Possible Duplicate: Why did we bother with line numbers at all? I'm curious about
This is not duplicate, as it may seam from the title. Continue reading. So
Disclaimer While this question looked like a potential duplicate , it was resolved by
Well, I hope this is not a duplicate, the search did not yield anything
This is a duplicate of this question, but specpifically regarding mercurial. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.