Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refer to the current object in an iterator

Tags:

java

iterator

I am trying to implement a search method in a TreeSet. By using an iterator with a condtional I would like to be able to run through the set and print the object that matches the condition. However the way I am doing it at the moment is printing out the subsequent object rather than the current. This is what I have so far:

public void getDetails() {         Iterator<Person> it = this.getPersonSet().iterator();         System.out.println("Enter First Name");         String first = in.next().toLowerCase();         System.out.println("Enter Second Name");         String last = in.next().toLowerCase();          while (it.hasNext()) {             if (it.next().getLast().toLowerCase().equals(last)) {                 Person p = it.next();                 System.out.println(p);             }         }      } 

Any help would be great

like image 415
Robairto Avatar asked Mar 02 '11 01:03

Robairto


People also ask

What is the syntax of iterator?

Iterator object can be created by calling iterator() method present in Collection interface. Syntax: Iterator itr = c. iterator();

Is an iterator an object?

In JavaScript an iterator is an object which defines a sequence and potentially a return value upon its termination. Specifically, an iterator is any object which implements the Iterator protocol by having a next() method that returns an object with two properties: value.

Which methods should an object have to be an iterator object?

Technically speaking, a Python iterator object must implement two special methods, __iter__() and __next__() , collectively called the iterator protocol. An object is called iterable if we can get an iterator from it. Most built-in containers in Python like: list, tuple, string etc.


2 Answers

This is what you would want to do:

while (it.hasNext()) {             Person p = it.next();             if (p.getLast().toLowerCase().equals(last)) {                 System.out.println(p);             }         } 
like image 117
rkg Avatar answered Sep 19 '22 19:09

rkg


How do I refer to the current object in an iterator

For the record, the Iterator API does not allow you to do this. There is no notion of a "current" object. The Iterator.next() method gives you the next object ... and moves on.

(The ListIterator.previous() and ListIterator.next() methods are analogous. Note that in the ListIterator case, method behaviour is documented in terms of a cursor that denotes a position before / between / after elements in the sequence being iterated.)

The solution is to assign the result of calling it.next() to a temporary variable, as described by the accepted answer.


I don't know for sure why the designers didn't include the notion of a "current" object in the API, but I can think of a couple of reasons:

  • It would make a typical iterator object bigger; i.e. an extra field to hold the current object.
  • It would mean more 1 more method for an Iterator class to implement.
  • The notion of a current object does not fit well with the "cursor" model documented in the ListIterator interface ... and implied by the current Iterator design.
  • There is a minor issue of the Iterator "hanging onto" the current object, thereby preventing it from being GC'ed.
  • The large majority of iterator use-cases don't require a current object.
  • There are other ways to deal with this.

Sounds like a good call ...

like image 40
Stephen C Avatar answered Sep 19 '22 19:09

Stephen C