Iterating over a hashmap within GWT

I have seen several searches looking for information on hashmaps within the Google Web Toolkit.  As part of the ‘String-based RPC’ sample code (found here), I use hashmaps to store pending requests.  If a call to HTTPRequest.AsyncPost fails (i.e. returns false, a condition which I have never seen and from what I recall from looking through the JAR, won’t happen), the code iterates over the hashmap to call the onFailure methods of the RPC’d objects.  ServerComm.java has the loop about two-thirds of the way down.  It takes a bit of casting to make it happen.

Specifically, it is:

for (Iterator i=waitingReqs.entrySet().iterator(); i.hasNext(); ) {
((ServerData)((Map.Entry)i.next()).getValue()).onFailure(”Call failed”);
}

where waitingReqs is a HashMap.  To get an iterator, you first need to get the entrySet for the HashMap.  This is a set containing key/value pairs as Map.Entry objects.  The Iterator returns a specific Map.Entry.  I only need the value, since in this case the key is unimportant.

As a further note, there should probably be @gwt.TypeArgs definitions for the HashMap variables specifying the data types stored within the HashMap.  It works without them, but it would be better form to include them.

Comments are closed.