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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:14:57+00:00 2026-06-06T12:14:57+00:00

I have a question regarding the iterator behavior in Java. I have a call

  • 0

I have a question regarding the iterator behavior in Java.

I have a call such as this:

myIterable.iterator().hasNext()

If this call returns true, can I be sure that the collection has at least two elements?
From the Java API specification, I could only find out that true means there is one more element to go which can be reached by next(). But what happens if the pointer is at the very beginning (meaning whether the hasNext() can recognize the first element separately)

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html

it says true if the iteration has more elements. But more element could also mean the very first one?

[Edit]

How can I know whether the iterator has exactly two elements to iterate through? Of course, I can iterate and count, but I can’t go back or iterate twice or clone the iterator in my case, this is an Hadoop iterator.

  • 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-06T12:15:00+00:00Added an answer on June 6, 2026 at 12:15 pm

    hasNext() returning true or false makes you able to discern between zero items and one-or-more (at the start of the iteration that is).

    You want to know whether it has two-or-more, without starting the iteration. I think, basically, you can not. Exposing that information is more than an iterator has to do: make available the next and only the next item (and information about whether this item exists).

    Possibly, the iterator itself doesn’t even have that knowledge yet!

    But of course you are free to memorize the items you already took out of the list. Then you could use them later, once you know there’s actually two or more items in the list.

    If you need to pass the iterator to other code, you could write your own class that implements iterator, internally remembers the first two items as member variables and hands out those first, then continues to iterate over the rest of the items in the original iterator (if there’s any more left) – a reference to the original iterator therefore also needs to be stored in your custom made iterator

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

Sidebar

Related Questions

I have a question regarding inheritance in Java. If I have this base class
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have a question regarding OpenGL glBufferData() method.Can I pass the vertex data in
I have question regarding polymorphism about its assignment statement, for Example this is The
I have a question regarding threading in Java. In my code, ... client.doSth(); //
Have a question regarding URL and jQuery. Can I specify URL to tell jQuery
I have a question regarding vectors: If I have a std::vector<MyClass> will this vector
I have question regarding Java Comparator. Or maybe you all have another Idea to
I have question regarding RESTful resources similar to this other question . We're also
I have a question regarding GetTickCount function, I have two calls to this function

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.