*
The moose likes Java in General and the fly likes Reseting an Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reseting an Iterator" Watch "Reseting an Iterator" New topic
Author

Reseting an Iterator

Amit Chohan
Greenhorn

Joined: Oct 31, 2002
Posts: 29
Hello All
Quick question -
Is there a way I can reset an iterator, for example, i want the following part of code to search a TreeSet(store) for matching words -

I want the iterator to return to the start of store(TreeSet) so that I can search it again for matching word.
Thanks in advance
Amit Chohan


Java Junior<br />Leicester<br />England
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Iterator has no reset method. To start again at the beginning, just get a new Iterator by calling iterator() again.
For this program, I think you'll find it's much simpler if you use the contains() method. There shouldn't really be any need to iterate through the whole TreeSet just to look for a given String.


"I'm not back." - Bill Harding, Twister
Amit Chohan
Greenhorn

Joined: Oct 31, 2002
Posts: 29
Thank you for your reply.
If i use the contains method it will return either true or false. I will still have to iterate through the collection to find the Objects whos ArrayList i want to update.
The Objects consist of 'String word' and 'ArrayList lineNumber'. Each time i find an object which contains the word from the input file i add the line number to its ArrayList.
To over come the iterator problem i tried putting the line

in the loop but it throws the following error
java.util.ConcurrentModificationException
at java.util.TreeMap$Iterator.next(TreeMap.java:1023)
at WordIndex2.main(WordIndex2.java:64)
Not sure what it means?
Amit Chohan
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It seems to me that get() would be a much easier way to retrieve the element you need from the TreeMap. It's probably more efficient, too.


Java API Documentation
The Java Tutorial
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Are you iterating over a TreeSet or TreeMap? Oh wait - a TreeSet actually has a TreeMap inside it; that's probably why TreeMap appears in your stack trace. Hrmmm... TreeSet doesn't actually have a get() (which would've been my suggestion too if you were using a TreeMap).
Hmmm... I have a hard time offering advice here without a better understanding of what the code is trying to do. The Word class - do the equals() and compareTo() methods make use of the line count info? I suspect that what you really want/need here is a Map in which the keys are all String objects, and the values are either Integers (representing a total count) or some sort of List-type structure containing mulptiple word counts and any other info a Word needs to know. I can't really say more though without a better understanding of the problem.
The ConcurrentModificationException indicates that there's more than one thread in your program, and they're modifying the TreeSet at the same time, without proper synchronization. You will need to study your code to see what other threads are doing, and then probably add some sort of synchronization to prevent the concurrent access. The simplest way to do this is probably to use Collections.synchronizedCollection() to get a synchronized view of the TreeSet. Be sure to read the API for this method, as it contains important info about how to synchronize while iterating. Note that there's a good chance this isn't really the best way to fix your code, just the simplest. Other solutions depend on how your code is organized ande what it's trying to do. I suggest studying the whole concept of threads and synchronization very carefully; a quick fix is rarely the correct solution.
Amit Chohan
Greenhorn

Joined: Oct 31, 2002
Posts: 29
Thank you Layne and Jim for finding the time to reply. I will be looking into both of these suggestions.
Amit Chohan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reseting an Iterator