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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:45:59+00:00 2026-06-13T12:45:59+00:00

As a C# programmer I have a sketchy understanding of Java / Scala iterator

  • 0

As a C# programmer I have a sketchy understanding of Java / Scala iterator design.

I am trying to (lazily – for the source may be big) read records from a RecordReader (in some third party library). I need to do some additional work every 100 records.

for (group <- reader.iterator.zipWithIndex.grouped(100)) {
  for ((record, i) <- group) {
    println(i + "|" + record.key)
  }
  // ...
}

This gives me the very last record, repeatedly, each time.

If I don’t use grouped, it works fine and I get each record. Am I missing something about lazy streaming or Java iterators?

  • 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-06-13T12:45:59+00:00Added an answer on June 13, 2026 at 12:45 pm

    To troubleshoot, try to decorate your iterator in an another iterator that prints what is going on:

    def wrap[T](i: Iterator[T]) = new Iterator[T] {
      def hasNext = { val b = i.hasNext; println("hasNext => " + b); b }
      def next() = { val n = i.next(); println("next() => " + n); n }
    }
    
    val reader = Iterator.from(20).take(10).toList
    for (group <- wrap(reader.iterator).zipWithIndex.grouped(5)) {
      for ((v, i) <- group) println("[" + i + "] = " + v)
    }
    

    Call wrap on the iterator, the very first time you instantiate the iterator. This will print something like:

    hasNext => true
    hasNext => true
    next() => 20
    hasNext => true
    next() => 21
    hasNext => true
    

    This should help you determine if the iterator is ill behaved… It could be for instance that the library does not deal correctly with calling hasNext multiple times without calling next. In that case you can modify wrap so that you make the iterator behave correctly. One more thing, from the symptoms, it feels like you’ve already consume the iterator before the grouped is called. So be extra careful and check if you’ve used the same iterator reference before.

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

Sidebar

Related Questions

Scala programmer should have known that this sort of writing : class Person{ var
I'm not a C++ programmer and have difficulty understanding the explanations given on websites.
I have another programmer who I'm trying to explain why it is that a
I am a Java programmer and I have always created separate files for Classes,
There are many skills a programmer could have (understanding the problem, asking good questions,
I am trying to be a good programmer and have unit tests for my
Since I'm not an Php programmer I have to ask you, I have to
I have a programmer who is using VB and LINQ; and I have a
I am a C# programmer but I have to work with some VB.Net code
Hi all fellow programmer I'd like to have a separate js file for each

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.