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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:04:18+00:00 2026-05-26T21:04:18+00:00

The code is pretty simple: List lst1 = new ArrayList(){{add(1);}}; Iterator it = lst1.iterator();

  • 0

The code is pretty simple:

    List lst1 = new ArrayList(){{add("1");}};
    Iterator it = lst1.iterator();
    System.out.println(it.hasNext());
    it.next();
    System.out.println(it.hasNext());
    lst1.remove(0);
    System.out.println(it.hasNext());

I know that you should never remove from a collection while iterating through its iterator, please don’t tell me that.

Instead, please give me a sane explanation why would a remove() operation change the value returned by hasNext() from false to true:

$ /usr/lib/jvm/sun-jdk-1.6/bin/java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode)

true
false
true

hasNext() API documentation clearly states that it should return true ONLY if there are more elements in the iterator, and calling next() returns an element. The output proves that it’s not the case with SUN JDK 1.6

IBM JDK doesn’t do that, hasNext returns false after remove() gets called:

$ /usr/lib/jvm/ibm-jdk-bin-1.6/bin/java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build pxi3260sr9fp2-20110625_01(SR9 FP2))
IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux x86-32 jvmxi3260sr9-20110624_85526 (JIT enabled, AOT enabled)

true
false
false

Is there a bug in Sun/Oracle hasNext() method? Or, is there a bug in IBM hasNext() method? Or, should I just not bother with all that?

  • 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-26T21:04:19+00:00Added an answer on May 26, 2026 at 9:04 pm

    From Iterator.remove():
    “The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.”

    So it is unspecified, meaning that NOTHING is a bug. It could format your harddisk in this case, and still not be incorrect.

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

Sidebar

Related Questions

Client code is pretty simple: <form action=DDServlet method=post> <input type=text name=customerText> <select id=customer> <option
Pretty simple code, I have a tuple extraAccessions with two values, a string and
I'm trying to prototype some pretty simple Hibernate code originating from a tutorial and
The question is pretty simple, the code below shows the items in one category.
Pretty simple task, but the source code doesn't do required job... Please advise. There
I'm a newbie at Flash, so started playing with a pretty standard code sample:
I use Visual Studio's Code Snippet feature pretty heavily while editing c# code. I
I just came onto a project with a pretty huge code base. I'm mostly
It's pretty difficult to show code for ASP.NET here, so I will try my
I am pretty sure the following button-activated form code should raise a Control-F12 in

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.