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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:24:44+00:00 2026-05-30T18:24:44+00:00

I am doing a research on java iterator interface and cannot understand why it

  • 0

I am doing a research on java iterator interface and cannot understand why it is designed like that.

Why java iterator use hasNext and next instead merge them into one method?

this a typical usage of java iterator

Iterator iter = //iterator from a list
while(iter.hasNext()){
    Object obj = iter.next();
    // do something to obj
}

why not

Iterator iter = //iterator from a list
Object obj = null;
try {
    while(true){
        obj = iter.next();
        // do something to obj
    }
} catch (NoSuchElementException e) {}

It is clear this approach looks ugly but what happen if next return null for when reach to end instead throw an exception? than code can be simplify to

Iterator iter = //iterator from a list
Object obj = null;
while((obj = iter.next()) != null){
    // do something to obj
}

this is how NSEnumerator in Objective-C works

NSEnumerator *enumerator = // from an array
while (id obj = [enumerator nextObject]) {
    // do something to obj
}

This increase the overhead of implement custom iterator.

This also make java iterator not thread-safe. For example an ArrayList have one element in it. Two threads both ask for a same iterator for that list hasNext at same time. Than both threads will see true and they will invoke next on that iterator. Because there is only one element and iterator has been asked twice which definitely will lead to an exception or error state.

I know there are thread-safe iterator but I am not sure hot it implement but I think lots blocking are happening which make it inefficient.

I think problem is that check and update are not happen atomically and I cannot understand why java designed iterator interface like that.


Update

I see that null can be a value so my approach is invalid. But is any possible workaround to the problems I mentioned above?

  • 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-30T18:24:45+00:00Added an answer on May 30, 2026 at 6:24 pm

    Your proposition would make it impossible to have null values in collections, since it uses null as a “poison pill” to detect the end of the iteration.

    In the very very rare cases two threads share an iterator, you just need to wrap it inside some custom class and synchronize the access to the iterator, to make the check-then-act operation atomic. This is needed anyway since, even if the iterator just had one method, the backing collection (ArrayList in your example) is not thread-safe.

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

Sidebar

Related Questions

I've got a Java/Flex project that I'm building using Maven. After doing some research
I have a blogengine.net install that requires privatization. I'm doing research work at the
I'm doing some research into using log4net, and I found the IObjectRenderer interface interesting.
I am doing some research related to Java NIO. I need to find some
Background: I'm doing machine learning research, and want to use the FANN library to
I'm doing some research in how to implement a event-handling scheme in C++ that
I've been doing some research for a mathematical Android related project I'd like to
I am doing research on a SOA topic and i am trying to understand
I'm doing research on what kind of technology to use for a web application
I'm starting a Web Application project in Java and I've been doing some research

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.