Which is better between using long string key or short string key in HashMap?
Example:
1. Long string key in HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put("[ART.117.4002] ADAPTER RUNTIME (ADAPTER SERVICE): UNABLE TO INVOKE ADAPTER SERVICE", "Cannot invoke adapter service");
Note: the long string would be limited to maximum 120 chars and all is uppercased. If the length is more than the max. chars, it will be truncated.
2. Short string key in HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put("B8B77715", "Cannot invoke adapter service");
Note: the B8B77715 is a CRC32 of "[ART.117.4002] ADAPTER RUNTIME (ADAPTER SERVICE): UNABLE TO INVOKE ADAPTER SERVICE".
Let’s say there would be 4000+ entries in the HashMap. Which is better between two in term of performance?
A CRC32 is a rough approximation of your original value, but it will be possible for two different original values to result in the same CRC32 value. This makes them a very poor candidate for a key to a
HashMapand the fact it reduces data integrity should trump any potential performance concerns. Definitely use[ART.117.4002] ...— why introduce a potential (if rare) bug when you don’t need to?That being said, the part in the beginning (between the square brackets) looks like it has the potential for being a unique identifier. If that were so, you could see some (quite marginal) performance boosts by using just the token between the brackets (via string parsing) rather than that whole big string.