Why define object for getEntry in HashMap(为什么要在 HashMap 中为 getEntry 定义对象)
问题描述
我是泛型新手,我不确定我的问题的答案是 opinion based
还是有真正的原因.在下面的代码中,需要对对象条目的键进行大小写?
I am new to generics and I am not sure if the answer to my question is opinion based
or has a genuine reason. In the following code what was need to case a key of an entry to an object ?
Object k;
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
它似乎很容易被替换为
if (e.hash == hash && (e.key == key || (key != null && key.equals(e.key))))
更多参考:
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
推荐答案
这是一种极端的优化措施,对于通用编程实践来说可能不是必需的.这是一个可以回答的 讨论你的问题.以下声明是从该帖子中复制的:
This is an extreme optimization measure that is probably not necessary for general purpose programming practice. Here is a discussion that could answer your question. Below statement is copied from that post:
这是 Doug Lea 流行的一种编码风格.这是一个可能没有必要的极端优化;您可以期望 JIT 进行相同的优化.(您可以尝试自己检查机器代码!)不过,复制到本地会产生最小的字节码,对于低级代码,最好编写更接近机器的代码.
It's a coding style made popular by Doug Lea. It's an extreme optimization that probably isn't necessary; you can expect the JIT to make the same optimizations. (you can try to check the machine code yourself!) Nevertheless, copying to locals produces the smallest bytecode, and for low-level code it's nice to write code that's a little closer to the machine.
这篇关于为什么要在 HashMap 中为 getEntry 定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么要在 HashMap 中为 getEntry 定义对象
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01