Tbh. I think you didn't look really hard...a simple search of "java iterator" on google returns multiple examples...
Anyway, usage is fairly simple. If you have a class that implements the "Iterator" interface (like most of the Collection classes), you
1) obtain the Iterator by calling the iterator() method of the collection
2) you loop over the collection using the hasNext() method of the Iterator
3) in the loop body, you obtain the current element by using the next() method of the Iterator (you will likely have to cast to the type of the objects in the collection)
4) then you can use the object normally. Note: if if you want to call the remove() method of the Iterator, you will have to make sure no other threads access the collection at the same time (synchronisation)!
Joined: Nov 23, 2009
Hello to you D. Ogranos,
you were right I did not do a good researching... now I did that and found following:
source: Sun docs, from what I have understood the Iterator interface has 3 methods only remove(), hasNext() and Next(), right?