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

  • Home
  • SEARCH
  • 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 6955917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:47:59+00:00 2026-05-27T14:47:59+00:00

Possible Duplicate: Why is Java's Iterator not an Iterable? We all know java’s extended

  • 0

Possible Duplicate:
Why is Java's Iterator not an Iterable?

We all know java’s extended for loop:

List<X> list = ...
for(X x : list) { ... }

What I need is:

Iterator<X> listIterator = ...
for(X x : listIterator){ ... }

Java does not allow this. I was wondering if there’s a good reason why the specification does not support this.

Here’s my usecase:

I’m writing a reader for a kind of file format. Such a file contains entries that are to be read. For the sake of the example assume that I’m trying to reinvent BufferedReader and my elements are Strings.

I’m quite unhappy with the API style of the original BufferedReader which forces me to write ugly code like:

for(String line = reader.readLine(); line != null; line = reader.readLine(){
   ...
}

I’d rather have something nice like

for(String line : reader){
   ...
}

Of course I can make my BufferedReader implement Iterable<String>. But this implies that there is a iterator() method that might be called several times. Since I cannot seek in the file, I cannot really support multiple parallel iterators.

Having my BufferedReader implement Iterator<String> instead of Iterable<String> seems far more reasonable. But then I can’t use the for statement 🙁

  • 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-27T14:48:00+00:00Added an answer on May 27, 2026 at 2:48 pm

    Because the contract for the foreach loop is to iterate over every element. If it was allowed to just take an Iterator you could give it a half-consumed Iterator and it wouldn’t know the difference.

    Set<String> set = new HashSet(Arrays.asList(new String [] {"One", "Two", "Three"}));
    Iterator i = set.iterator();
    // Consume the first.
    String first = i.next();
    for ( String s : i ) { // Error here.
    
    }
    

    I accept that it would make things easier but it could also introduce some serious bugs because you wont know you have covered all entries in the collection.

    // Works fine because Set is Iterable and the contract is satisfied 
    // that this will iterate over <b>all</b> of the entries.
    for ( String s : set ) {
    }
    

    You can always hack it around by making a transient Iterable. I cant test this right now but something like:

    for ( String s : new Iterable { 
      public iterator<String> () {
        return set.iterator ();
      }
    } ) {
    }
    

    Cumbersome but it should work.

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

Sidebar

Related Questions

Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: How do you find all subclasses of a given class in Java?
Possible Duplicate: Java - why no return type based method overloading? The compiler does
Possible Duplicate: Best Java obfuscator ? For my example I know that eclipse offers
Possible Duplicate: Java garbage collector - When does it collect? When people say that
Possible Duplicate: Java: convert List<String> to a join()d string Having this: List<String> elementNames =
Possible Duplicate: Java: Start Mail-Client with Attachment? I need to create a java code
Possible Duplicate: Java: Rationale of the Object class not being declared abstract Why is
Possible Duplicate: Java generics and array initialization How does one instantiate an array of
Possible Duplicate: Java - Distinct List of Objects i have a sorted array of

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.