Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Reseting an Iterator

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amit Chohan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Layne and Jim for finding the time to reply. I will be looking into both of these suggestions.
Amit Chohan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic