【hash、哈希散列、sha-1 -- 实践】【文章知识点很全面】抽空弄懂【hashCode源码 -- 翻译】【hashCode:强ID, equals:弱相等?】

1、hashCode(方法源码)

int java.lang.Object.hashCode()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

// 返回该对象的hash值。该方法可谓hashtable提供便利,比如hashmap中的。

The general contract of hashCode is:
•Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

在一个java程序运行中,一个对象的hashcode方法调用多次都必须返回相同的整数值,假设没有在equals方法中的信息被修改。但是在同一个应用的不同执行环节,这个整数值不必保持相等。
•If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

重点:假设2个对象通过equals(自反性、对称性、传递性、持久性)方法相等,那么调用hashCode返回值必须相等

•It is not required that if two objects are unequal according to the java.lang.Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

但是,2个对象通过equals返回不相等,不必要求调用hashCode必须返回不同观点值。但是程序员应该意识到对于不相等的对象返回不同的hashCode能提高hash tables的执行效率。

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)

作为一个合理的实践,不同Object的hashCode方法能返回不同的hash值。(通常对象的hashcode是由对象内部地址转换而来[ 一次执行环境中,内部地址唯一??? ],但是这个实现技术不是 JPL 强制要求的)

2、HashMap【好文章,知识点很全面!】深入理解 hashcode 和 hash 算法

摘要

  • 二进制计算的一些基础知识
  • 为什么使用 hashcode
  • String 类型的 hashcode 方法
  • 为什么大部分 hashcode 方法使用 31
  • HashMap 的 hash 算法的实现原理(为什么右移 16 位,为什么要使用 ^ 位异或)
  • HashMap 为什么使用 & 与运算代替模运算?
  • HashMap 的容量为什么建议是 2的幂次方?
  • 我们自定义 HashMap 容量最好是多少?

3、默克尔树及默克尔根

为什么“默克尔树” 能支撑比特币的底层交易系统

由于每个节点上的内容都是哈希值,所以也叫“哈希树”。

发布了354 篇原创文章 · 获赞 2 · 访问量 2124

猜你喜欢

转载自blog.csdn.net/m0_37681589/article/details/100514120
今日推荐