[Home]PEZ/HelpRequest

Robo Home | PEZ | Changes | Preferences | AllPages

Stupid question. If I have a Java Map and add something to it using something like:
myMap.put(myStrOne.substring(a), myWave);
Shouldn't I be able to retreieve myWave using something like:
Wave w = (Wave)myMap.get(myStrTwo.substring(b));
? Assuming, of course, that the two String expressions produce equal strings. I'm trying to make a new gun and I stumble on this silly step...

-- PEZ

Yes you should (in more detail they would produce the same

hashcode()
and be
equals()
). You sure about them being equal? :) Have to ask you know :) " The substring begins with the character at the specified index and extends to the end of this string. " Just to be sure that you used index you think you are using. It's easy to assume that the single argument version is from the beginning to the index instead for example... -- Pulsar

I would also highly suggest you check the case of the characters. That always seems to catch me. -- jim

The string is just a pattern for my pattern matching. I had some other error in my code that I didn't manage to track down. Now I get the expected behaviour from my Map usage. Thanks for offering your help both of you. Totally appreciated. My gun isn't really top performing yet, but at least now I can start tweaking it. -- PEZ

An interesting note about using Java maps if you're not using strings as keys - If you use a HashMap?, you won't be able to dependably retrieve objects unless the class of the key overrides hashCode() (and probably equals(), like Pulsar said), and if you use a TreeMap?, you must implement the Comparable interface in the key class for it to work correctly. Quite a few of the standard libraries already do these things (like String), but it's something to make sure about. -- Kawigi

Yeah, that's why I use String objects as keys. Though implementing hashCode() and equals() isn't too much of a bother. But String objects work as good as anything else for the purposes I use them for here, -- PEZ


Robo Home | PEZ | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited July 24, 2004 20:37 EST by PEZ (diff)
Search: