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 4036576
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:14:24+00:00 2026-05-20T12:14:24+00:00

Suppose I have an iterator: val it = List(a,b,c).iterator I want a copy of

  • 0

Suppose I have an iterator:

val it = List("a","b","c").iterator

I want a copy of it; my code is:

val it2 = it.toList.iterator

It’s correct, but seems not good. Is there any other API to do 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-20T12:14:25+00:00Added an answer on May 20, 2026 at 12:14 pm

    Warning: as of Scala 2.9.0, at least, this leaves the original iterator empty. You can val ls = it.toList; val it1 = ls.iterator; val it2 = ls.iterator to get two copies. Or use duplicate (which works for non-lists also).

    Rex’s answer is by the book, but in fact your original solution is by far the most efficient for scala.collection.immutable.List’s.

    List iterators can be duplicated using that mechanism with essentially no overhead. This can be confirmed by a quick review of the implementation of iterator() in scala.collection.immutable.LinearSeq, esp. the definition of the toList method, which simply returns the _.toList of the backing Seq which, if it’s a List (as it is in your case) is the identity.

    I wasn’t aware of this property of List iterators before investigating your question, and I’m very grateful for the information … amongst other things it means that many “list pebbling” algorithms can be implemented efficiently over Scala immutable Lists using Iterators as the pebbles.

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

Sidebar

Related Questions

Suppose I have a hash_map and a code like // i is an iterator
Suppose I have a list of sets and I want to get the union
Suppose we have an iterator (an infinite one) that returns lists (or finite iterators),
Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots;
Suppose I have a function which can either take an iterable/iterator or a non-iterable
Suppose I have a list, in which no new nodes are added or deleted.
Suppose I have this code... class GraphFactory : public QObject { private: QMap<QString, IGraphCreator*>
Suppose I have a std::vector<Obj *> objs (for performance reasons I have pointers not
Suppose I have a doubly linked list. I create it as such: MyList list
suppose I have the following source table (called S): name gender code Bob 0

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.