Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

At which length is a String key of a HashMap considered bad-practice?

Tags:

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.

  • Is there an unwritten law to the length of the HashMap key?
  • Is it considered bad practice to have String keys of let's say 150 characters?
  • Does it affect performance? At which length?
like image 539
Caroline Avatar asked May 12 '13 10:05

Caroline


People also ask

Can HashMap have string as key?

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).

What is the limit of HashMap?

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.

What is the best key for HashMap?

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.

How big should a HashMap be?

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.


2 Answers

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.

like image 93
pcalcao Avatar answered Oct 11 '22 07:10

pcalcao


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?

like image 29
Peter Lawrey Avatar answered Oct 11 '22 07:10

Peter Lawrey