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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:14:34+00:00 2026-05-14T06:14:34+00:00

I read Why is Java's Iterator not an Iterable? and Why aren't Enumerations Iterable?

  • 0

I read Why is Java's Iterator not an Iterable? and Why aren't Enumerations Iterable?, but I still don’t understand why this:

void foo(Iterator<X> it) {
  for (X x : it) {
    bar(x);
    baz(x);
  }
}

was not made possible. In other words, unless I’m missing something, the above could have been nice and valid syntactic sugar for:

void foo(Iterator<X> it) {
  for (X x; it.hasNext();) {
    x = it.next();
    bar(x);
    baz(x);
  }
}
  • 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-14T06:14:35+00:00Added an answer on May 14, 2026 at 6:14 am

    but I still don’t understand why this […] was not made possible.

    I can see several reasons:

    1. Iterators are not reusable, so a for/each would consume the iterator – not incorrect behavior, perhaps, but unintuitive to those who don’t know how the for/each is desugared.
    2. Iterators don’t appear “naked” in code all that often so it would be complicating the JLS with little gain (the for/each construct is bad enough as it is, working on both Iterables and arrays).
    3. There’s an easy workaround. It may seem a little wasteful to allocate a new object just for this, but allocation is cheap as it is and escape analysis would rid you even of that small cost in most cases. (Why they didn’t include this workaround in an Iterables utility class, analogous to Collections and Arrays, is beyond me, though.)
    4. (Probably not true – see the comments.) I seem to recall that the JLS can only reference things in java.lang[citation needed], so they’d have to create an Iterator interface in java.lang which java.util.Iterator extends without adding anything to. Now we have two functionally equivalent iterator interfaces. 50% of the new code using naked iterators will choose the java.lang version, the rest use the one in java.util. Chaos ensues, compatibility problems abound, etc.

    I think points 1-3 are very much in line with how the Java language design philosophy seems to go: Don’t surprise newcomers, don’t complicate the spec if it doesn’t have a clear gain that overshadows the costs, and don’t do with a language feature what can be done with a library.

    The same arguments would explain why java.util.Enumeration isn’t Iterable, too.

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

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The attribute name passed to .attr() needs to be in… May 16, 2026 at 8:39 am
  • Editorial Team
    Editorial Team added an answer C++ doesn't use RAII. You CAN use RAII in your… May 16, 2026 at 8:39 am
  • Editorial Team
    Editorial Team added an answer Did you try OmniThreadLibrary? May 16, 2026 at 8:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have read Java Concurrency in Practice and this is a great reference, but
I'm using Clojure, but I can read Java, so this isn't a Clojure specific
I've read Java does not support static local variables unlike C/C++. Now if I
From what I have read java (usually) seems to compile java to not very
The java.lang.Iterator interface has 3 methods: hasNext , next and remove . In order
I'm wrapping a java.sql.RecordSet inside a java.util.Iterator. My question is, what should I do
I have to read xls file in java.I used poi-3.6 to read xls file
I've read and re-read Java Concurrency in Practice, I've read several threads here on
I know that you can use java.util.Properties to read Java properties files. See: Java
First of all, this is my code (just started learning java): Queue<String> qe =

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.