I try to pay attention to good performance and clean code all the time.
I'm having difficulties trying to grasp whether it's sane to have a HashMap with keys of 150 characters.
String is as a key of the HashMap When you pass the key to retrieve its value, the hash code is calculated again, and the value in the position represented by the hash code is fetched (if both hash codes are equal).
Is there a theoretical limit for the number of key entries that can be stored in a HashMap or does it purely depend on the heapmemory available ? Looking at the documentation of that class, I would say that the theoretical limit is Integer. MAX_VALUE (231-1 = 2147483647) elements.
There is no universal answer to which is the best key, since there is no best key. The best key for use in your hash map is one that you need for retrieval. Just make sure the key's hashCode() is quick and uses the int space appropriately.
Capacity is the number of buckets in the HashMap. Finally, the default initial capacity of the HashMap is 16. As the number of elements in the HashMap increases, the capacity is expanded.
Not really, 150 chars String is relatively trivial to calculate an hashCode
for.
That being said, in circumstances like this I would advise you to test it!
Create a routine that populates an HashMap with, say, insert a size here that is representative of your use scenario random values with 5 character strings as keys. Measure how long it takes. Then do the same for 15 character keys, and see how it scales.
Also, Strings in Java are immutable, which means that hashCode
can be cached for each String that is stored in the String Constant Pool, and doesn't need to be recalculated when you call hashCode on the same String object.
This means that although you're calculating larger hash codes when creating your map, on access many of those will already be pre-calculated and cached, making the size of the original String even less relevant.
Is there an unwritten law to the length of the HashMap key?
If there is, it is also unspoken. I would measure your use case in a profiler and only worry about the things you can measure as a problem, not the things you can imagine might be a problem.
Is it considered bad practice to have String keys of let's say 150 characters?
I doubt it.
Does it affect performance? At which length?
Everything affects performance, usually to small to matter or sometimes even measure. The question should be; do you need 150 character keys. If you do, then use them.
There is an exotic case where adding strings with hashCode() of zero is a bad idea. This is because in Java 1.0 to 6 doesn't optimise the use case of a hashCode of zero and it can be predicted for denial of service attacks. Java 7 fixes this by having a secondary, less predictable hashcode.
Why doesn't String's hashCode() cache 0?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With