Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterator next method

In ResultSet interface there is next method which return boolean and then you can directly access the current record by get methods

Why Iterator from java.util just not drop hasNext() method and have only next method which will move the cursor to next element and return boolean?

like image 596
Delta Avatar asked Mar 14 '12 17:03

Delta


People also ask

Is there a next method in iterator?

Iterator interface provides the following methods: boolean hasNext() - Returns true if the iteration has more elements. E next() - Returns the next element in the iteration. void remove() - Removes from the underlying collection the last element returned by the iterator (optional operation).

What does iterator next () returns?

2. next(): Returns the next element in the iteration. It throws NoSuchElementException if no more element is present.

What does iterator next () return in Java?

Returns true if the iteration has more elements. Returns the next element in the iteration. Removes from the underlying collection the last element returned by this iterator (optional operation).

How does iterator get next value?

The next() function returns the next item from the iterator. If the iterator is exhausted, it returns the default value passed as an argument.


2 Answers

Because next() currently returns the next item.

Java could have implemented the iterator pattern in the same way that .NET does, with a MoveNext() returning a Boolean value, and then a Current property with the "current" value - but it's still two members...

The other alternative is having one return value which encapsulates the two ideas of "is there a value" and "what is the value if there is one" - a sort of Maybe type, really. Of course in Java, that means allocating a new object on each iteration, which isn't ideal...

like image 139
Jon Skeet Avatar answered Sep 24 '22 09:09

Jon Skeet


Because you would still need another function to retrieve the value, so nothing would be gained.

In a ResultSet, next() gets you to the next record, or returns false if there is none. Then you do getString()'s, getInt()'s or whatever to retrieve field values. So you write code like:

while (rs.next())
{
  name=rs.getString("name"); // or whatever
  ... do something with name ...
}

In an Iterator, hasNext() returns a true or false to indicate if there's another record. Then you call next() to retrieve the value. so you write code like:

while (iter.hasnext())
{
  name=iter.next(); // or whatever
  ... do something with name ...
}

The pattern ends up being pretty similar. It's unfortunate that the implementations are inconsistent. That is, ResultSet.next not only tells you if you're at the end but also advances the positoin, while Iterator.hasNext tells you if you're at the end but doesn't advance the position. But actual use is pretty similar.

It wouldn't really work to try to combine Iterator.hasNext and Iterator.next into one function. How would you know when you reached the end? You could say that it returns null at the end ... but what if null is a valid value in the list? I suppose you could add some magic value, but you'd still have the problem of distinguishing the magic meaning from an entry in the list that just happened to have that value. Like if you have a list of ints, you could say that -1 means end-of-list, but then what if the list includes a value of -1?

The only other alternative I see is to have the next() function return an object that includes both an end-of-list indication and, if applicable, the value. But this would be a pain to use. You'd have to write something like:

while (true)
{
  IterableResult ir=iter.next();
  if (ir.end)
  {
    break;
  }
  else
  {
    name=ir.value;
    ... do something with name ... 
  }
}

That doesn't seem to gain anything.

Maybe there's another approach that would work better, but I can't think of one. And apparently the creators of the Iterator interface couldn't think of a better way either! :-)

like image 32
Jay Avatar answered Sep 24 '22 09:09

Jay