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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:31:48+00:00 2026-06-06T15:31:48+00:00

Possible Duplicate: Why is Java’s Iterator not an Iterable? Idiomatic way to use for-each

  • 0

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

Idiomatic way to use for-each loop given an iterator?

Can we use for-each loop for iterating the objects of Iterator type?

The foreach loop are as far as I know syntax sugar added in Java 5. So

Iterable<O> iterable;
for(O o : iterable) {
    // Do something
}

will essentially produce the same bytecode as

Iterable<O> iterable;
for(Iterator<O> iter = iterable.iterator(); iter.hasNext(); /* NOOP */) {
    O o = iter.next();
    // Do something
}

However, if I do not have an iterable in the first place, but only an iterator (say, because a class offers two different iterators), I cannot use the syntax sugar foreach loop. Obviously I can still do the plain old style iteration. However, I’d actually like to do:

Iterator<O> iter;
for(O o : iter /* Iterator<O>, not Iterable<O>! */) {
     // Do something
}

And of course I can do a fake Iterable:

class Adapter<O> implements Iterable<O> {
    Iterator<O> iter;

    public Adapter(Iterator<O> iter) {
        this.iter = iter;
    }

    @Override
    public Iterator<O> iterator() {
        return iter;
    }
}

(Which in fact is an ugly abuse of the Iterable API, as it can only be iterated once!)

If it were designed around Iterator instead of iterable, one could do a number of interesting things:

for(O o : iterable.iterator()) {} // Iterate over Iterable and Collections

for(O o : list.backwardsIterator()) {} // Or backwards

Iterator<O> iter;
for(O o : iter) {
    if (o.something()) { iter.remove(); }
    if (o.something()) { break; }
}
for(O : iter) { } // Do something with the remaining elements only.

Does anyone know why the language was designed this way? To avoid ambiguity if a class would implement both Iterator and Iterable? To avoid programmer errors that assume that "for(O o : iter)" will process all elements twice (and forget to get a fresh iterator)? Or is there some other reason for this?

Or is there some language trick I just do not know?

  • 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-06T15:31:50+00:00Added an answer on June 6, 2026 at 3:31 pm

    So I have a somewhat reasonable explanation now:

    Short version: Because the syntax also applies to arrays, which don’t have iterators.

    If the syntax were designed around Iterator as I proposed, it would be inconsistent with arrays. Let me give three variants:

    A) as chosen by the Java developers:

    Object[] array;
    for(Object o : array) { }
    Iterable<Object> list;
    for(Object o : list) { }
    Iterator<Object> iter;
    while(iter.hasNext()) { Object o = iter.next(); }
    

    The behaves the same way and is highly consistent across arrays and collections.
    Iterators however have to use the classic iteration style (which at least is not likely to cause errors).

    B) allow arrays and Iterators:

    Object[] array;
    for(Object o : array) { }
    Iterable<Object> list;
    for(Object o : list.iterator()) { }
    Iterator<Object> iter;
    for(Object o : iter) { }
    

    Now arrays and collections are inconsistent; but arrays and ArrayList are very closely related and should behave the same way. Now if at any point, the language is extended to make e.g. arrays implement Iterable, it becomes inconsistent.

    C) allow all three:

    Object[] array;
    for(Object o : array) { }
    Iterable<Object> list;
    for(Object o : list) { }
    Iterator<Object> iter;
    for(Object o : iter) { }
    

    Now if we end up in unclear situations when either someone implements both Iterable and Iterator (is the for loop supposed to get a new iterator or iterate over the current – happens easily in tree-like structures!?!). A simple tie-braker ala “Iterable beats Iterator” unfortunately won’t do: it suddenly introduces runtime vs. compile time difference and generics issues.

    Now suddenly, we need to pay attention to whether we want to iterate over collections/iterables or arrays, at which point we have gained very little benefits at the cost of a big confusion.

    The way “for each” is in Java (A) is very consistent, it causes very little programming errors, and it allows for the possible future change of turning arrays into regular objects.

    There is a variant D) that would probably also work okay:
    for-each for Iterators only. Preferrably by adding a .iterator() method to primitive arrays:

    Object[] array;
    for(Object o : array.iterator()) { }
    Iterable<Object> list;
    for(Object o : list.iterator()) { }
    Iterator<Object> iter;
    for(Object o : iter) { }
    

    But this requires changes to the runtime environment, not just the compiler, and breaks backwards compatibility. Plus, the mentioned confusion is still present that

    Iterator<Object> iter;
    for(Object o : iter) { }
    for(Object o : iter) { }
    

    Only iterates over the data once.

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

Sidebar

Related Questions

Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Possible Duplicate: When do you use Java’s @Override annotation and why? From the javadoc
Possible Duplicate: Java Interfaces? What is the use of interface in java? Is interface
Possible Duplicate: java for-loop problem Why is the output of following code: for (float
Possible Duplicate: Java modifiers syntax and format You can put modifiers of variables or
Possible Duplicate: java PreparedStatement Can I make the prepared statement SELECT * FROM STUDENTS
Possible Duplicate: Java: Why not to start a thread in the constructor? How to
Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” When should you use: class MyThread
Possible Duplicate: Java Generics In Eclipse, I am given warnings for using 'rawtypes' and
Possible Duplicate: Java REPL shell Hey, Is there any way to execute Java code

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.