Bob
Bob

Reputation: 1001

Iterator behavior in Java

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.

Upvotes: 1

Views: 6593

Answers (8)

Damet Liu
Damet Liu

Reputation: 36

In the iterator, it has a field named cursor.This cursor init value is the collection object size.When you call the method next(), it will --. The method hasNext() check the cursor is equals to zero.Its zero return false, else return true.

But the list and map is not the same, their difference is the concrete realization, the original is the same.List check size, map check end node.

Upvotes: 0

Jochem
Jochem

Reputation: 2994

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

Upvotes: 3

Kalai Selvan Ravi
Kalai Selvan Ravi

Reputation: 2886

No. If hasNext() returns true means, that collection having atleast one element because the origin position of iterator should before the first element.

List l = new ArrayList();
l.add(1);
Iterator it = l.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}

Result will be 1 because origin position of iterator should before the first element. When we check it.hasNext() for the first time, it will return true because it is having one element. Then, print the element using it.next(). Now only, iterator in the first position. When we check it.hasNext() for the second time, it will return false.

Upvotes: 1

XiO
XiO

Reputation: 366

How can I know whether the iterator has exactly two elements to iteratte 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 hadoop iterable.

There is no way to do this. Maybe an iterator is wrong for your scenario.

Upvotes: 1

Pramod Kumar
Pramod Kumar

Reputation: 8014

If myIterable.iterator().hasNext() returns true it means there is at least one element and you can use next() to access that.

Upvotes: 1

Sam
Sam

Reputation: 2969

Java iterators are positioned before the fist element. What your expression myIterable.iterator().hasNext() shows is that there is at least one element.

Upvotes: 2

Michael Laffargue
Michael Laffargue

Reputation: 10304

hasNext tells you there is another element accessible with next(). So it just mean you have at least one element.

Upvotes: 1

Gergely Szilagyi
Gergely Szilagyi

Reputation: 3903

it can mean the first one. You can only be sure that it has more than zero element

Upvotes: 5

Related Questions