HashMap 中使用长字符串键和短字符串键哪个更好?
HashMap 中使用长字符串键和短字符串键哪个更好?
示例:
1. 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");
注意:长字符串最多不超过 120 个字符,并且全部大写。如果长度超过最大长度。字符,它将被截断。
2. HashMap 中的短字符串键
HashMap<String, String> map = new HashMap<String, String>();
map.put("B8B77715", "Cannot invoke adapter service");
注意:B8B77715 是“[ART.117.4002]适配器运行时(适配器服务):无法调用适配器服务”的 CRC32。
假设 HashMap 中有 4000 多个条目。就性能而言,两者哪个更好?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CRC32 是原始值的粗略近似值,但两个不同的原始值可能会产生相同的 CRC32 值。这使得它们成为
HashMap
的键的一个非常糟糕的候选者,并且它降低了数据完整性的事实应该胜过任何潜在的性能问题。一定要使用[ART.117.4002] ...
——为什么在不需要时引入潜在的(如果罕见的话)错误?话虽这么说,开头的部分(方括号之间)看起来有可能成为唯一标识符。如果是这样,您可以通过仅使用括号之间的标记(通过字符串解析)而不是整个大字符串来看到一些(相当微小的)性能提升。
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
HashMap
and 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.
很难想象这可能很重要。使用最有意义的东西。
It's hard to imagine it could possibly matter. Use whatever makes the most sense.