-
Website
http://blog.dhananjaynene.com -
Original page
http://blog.dhananjaynene.com/2008/04/turbocharge-your-string-keyed-hashmaps/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
washort
1 comment · 1 points
-
danfairs
1 comment · 1 points
-
cheftony
1 comment · 3 points
-
Paddy3118
2 comments · 3 points
-
dipankarsarkar
1 comment · 1 points
-
-
Popular Threads
Of course, this still seems like a pointless optimization. In my test of doing a million puts/gets, the difference in your two approaches about 100ms, or .104 milliseconds per operation. Not sure that would make a difference in most application contexts....
As to the static HashMap in the symbol class it is I am afraid a memory leak being a cache with no eviction strategy and one that can not be flushed, as a bonus getSymbol is not Thread safe! Static is indeed evil http://gbracha.blogspot.com/2008/02/cutting-out...
1. You should NEVER use new String(String). It forces the JVM to create a new String object.
2. String literals, such as "mykey", are already automatically interned by Java.
3. The call to intern is slow, since it performs a lookup in a hashtable containing ALL String literals.
4. Hashcode is already stored in String, and calculated the first time it is needed (see source code of String.java)