Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

quick hashtable question

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
I want to walk through a hashtable, value by value. This is usually how I do it (assuming my key is a String, which is usually is, at least for me):<br><br>for (Enumeration e = myHash.keys(); e.hasMoreElements();) {<br>&nbsp;&nbsp;&nbsp;&nbsp;String key = e.nextElement().toString();<br>&nbsp;&nbsp;&nbsp;&nbsp;Object value = myHash.remove(key);<br>&nbsp;&nbsp;&nbsp;&nbsp;/* ...do stuff... */<br>}<br><br>The problem is, that leaves me with an empty Hashtable. I'd like to use <FONT FACE=monospace>myHash.get(key)</font>, but that would create an infinite loop as <FONT FACE=monospace>e.hasMoreElements()</font> would always be true. Is there another better way to walk through a hashtable (without leaving an empty hashtable)? Or do I have to create a seperate method that will throw the results into a new hashtable and then pass the new one back by reference? Is there something better than <FONT FACE=monospace>e.hasMoreElements()</font>? Thanks for any help.. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
never mind. scratch that. The Enumeration object is the list of keys, That gets knocked off by e.nextElement() and not by removing objects. Sorry about that... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top