hash codes in java I : gneral usage and how to produce hash codes

What is the use of hash codes in Java? Java uses hash codes for the same reason described above—to efficiently retrieve data from hash based collections. If the objects of your class are not used as keys in a hash based collection, for example, in a Hashtable, HashMap, etc., you need not even worry about hash codes for your objects at all.

You can compute hash code for an object in Java. In case of an object, the pieces of information that will be used to compute the hash code are the pieces of information that make up the state of the object. Java designers considered the hash code for an object so important that they provided a default implementation to compute the hash code for an object in the Object class.

The Object class has a hashCode() method that returns an int, which is the hash code of the object. The default implementation of this method computes the hash code of an object by converting the memory address of the object into an integer. Since the hashCode() method is defined in the Object class, it is available in all classes in Java. However, you are free to override the implementation in your class. Here are the rules that you must follow when you override the hashCode() method in your class. Suppose there are two object references, x and y.

If x.equals(y) returns true, x.hashCode() must return an integer, which is equal to y.hashCode(). That is, if two objects are equal using the equals() method, they must have the same hash codes.
    If x.hashCode() is equal to y.hashCode(), it is not necessary that x.equals(y) returns true. That is, if two objects have the same hash codes using the hashCode() method, they do not have to be equal using the equals() method.
    If the hashCode() method is called on the same object multiple times in the same execution of a Java application, the method must return the same integer value. The hashCode() and equals() methods are closely tied. If your class overrides any of these two methods, it must override both for the objects of your class to work correctly in hash-based collections. Another rule is that you should use only those instance variables to compute the hash code for an object, which are also used in the equals() method to check for equality.

If your class is mutable, you should not be using objects of your class as keys in hash-based collections. If the object has been used as a key changes after their use, you will not be able to locate the object in the collection because locating an object in a hash based collection is based on its hash code. In such cases, you will have stranded objects in the collection.

How should you implement a hashCode() method for a class? Here are some guidelines to write the logic for the hashCode() method for your class, which is reasonable for most of the purposes:

Start with a prime number, say 37.

int hash = 37;

Compute the hash code value for each instance variable of primitive data types separately using the following logic. Note that you need to use only those instance variables in the hash code computation, which are also part of the equals() method logic. Let’s store the result of this step in an int variable code. Let’s assume that value is the name of the instance variable.

For byte, short, int, and char data types, use their integer value as

code = (int)value;

For long data type, use the XOR for two halves of 64-bit as

code = (int)(value ^ (value >>>32));

For float data type, convert its floating-point values to an equivalent integer value using

code = Float.floatToIntBits(value)

For double data type, convert its floating-point value to long using the doubleToLongBits() method of the Double class and then convert the long value to an int value using the procedure as described above for the long data type.

long longBits = Double.doubleToLongBits(value);
    code = (int)(longBits ^ (longBits >>>32));

For boolean data type, use 1 for true and 0 for false.

code = (value ? 1 : 0)

For a reference instance variable, use 0 if it is null. Otherwise, call its hashCode() method to get its hash code. Suppose ref is the name of the reference variable.

code = (ref == null ? 0: ref.hashCode());

Compute the hash code using the following formula. Using 59 in the formula is an arbitrary decision. Any other prime number, say 47, will work fine.

hash = hash * 59 + code;

Repeat the above three steps for all instance variables you want to include in your hashCode() computation.
    Finally, return the value contained in the hash variable from your hashCode() method.

The above method is one of the many ways, not the only way, to compute hash code of an object in Java. Consult a good textbook on computing hash codes if you need a stronger hash function. All primitive wrapper classes and String class override the hashCode() method to provide reasonably good implementations of hash functions.

Tip  Java 7 added a utility class java.lang.Objects. It contains a hash() method that computes the hash code for any number of values of any type. From Java 7, you are advised to use the Objects.hash() method to compute the hash code of an object. Please refer to “The Objects Class” section later in this chapter for more details.

时间: 2024-11-05 11:39:09

hash codes in java I : gneral usage and how to produce hash codes的相关文章

Java提高篇——通过分析 JDK 源代码研究 Hash 存储机制

阅读目录 通过 HashMap.HashSet 的源代码分析其 Hash 存储机制HashMap 的存储实现Hash 算法的性能选项HashMap 的读取实现HashSet 的实现 HashMap 和 HashSet 是 Java Collection Framework 的两个重要成员,其中 HashMap 是 Map 接口的常用实现类,HashSet 是 Set 接口的常用实现类.虽然 HashMap 和 HashSet 实现的接口规范不同,但它们底层的 Hash 存储机制完全一样,甚至 H

对一致性Hash算法,Java代码实现的深入研究

一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法和一致性Hash算法的算法原理做了详细的解读. 算法的具体原理这里再次贴上: 先构造一个长度为232的整数环(这个环被称为一致性Hash环),根据节点名称的Hash值(其分布为[0, 232-1])将服务器节点放置在这个Hash环上,然后根据数据的Key值计算得到其Hash值(其分布也为[0, 232-1]),接着在

【数据结构与算法】一致性Hash算法及Java实践

追求极致才能突破极限 一.案例背景 1.1 系统简介 首先看一下系统架构,方便解释: 页面给用户展示的功能就是,可以查看任何一台机器的某些属性(以下简称系统信息). 消息流程是,页面发起请求查看指定机器的系统信息到后台,后台可以查询到有哪些server在提供服务,根据负载均衡算法(简单的轮询)指定由哪个server进行查询,并将消息发送到Kafka,然后所有的server消费Kafka的信息,当发现消费的信息要求自己进行查询时,就连接指定的machine进行查询,并将结果返回回去. Server

【转载】对一致性Hash算法,Java代码实现的深入研究

原文地址:http://www.cnblogs.com/xrq730/p/5186728.html 一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性Hash算法的算法原理做了详细的解读. 算法的具体原理这里再次贴上: 先构造一个长度为232的整数环(这个环被称为一致性Hash环),根据节点名称的Hash值(其分布为[0, 232-1])将服务器节点放置在这

【转】【java源码分析】Map中的hash算法分析

全网把Map中的hash()分析的最透彻的文章,别无二家. 2018年05月09日 09:08:08 阅读数:957 你知道HashMap中hash方法的具体实现吗?你知道HashTable.ConcurrentHashMap中hash方法的实现以及原因吗?你知道为什么要这么实现吗?你知道为什么JDK 7和JDK 8中hash方法实现的不同以及区别吗?如果你不能很好的回答这些问题,那么你需要好好看看这篇文章.文中涉及到大量代码和计算机底层原理知识.绝对的干货满满.整个互联网,把hash()分析的

一致性hash算法及java实现

一致性hash算法是分布式中一个常用且好用的分片算法.或者数据库分库分表算法.现在的互联网服务架构中,为避免单点故障.提升处理效率.横向扩展等原因,分布式系统已经成为了居家旅行必备的部署模式,所以也产出了几种数据分片的方法: 1.取模,2.划段,3.一致性hash 前两种有很大的一个问题就是需要固定的节点数,即节点数不能变,不能某一个节点挂了或者实时增加一个节点,变了分片规则就需要改变,需要迁移的数据也多. 那么一致性hash是怎么解决这个问题的呢? 一致性hash:对节点和数据,都做一次has

java.util.HashMap源码浅析之解决hash冲突

HashMap是java无论是企业管理系统还是web或者其他应用层的程序开发,都是应用比较多的一种数据结构,正好最近面试有问到与HashMap解决hash冲突的方式(本人菜比没答上来),现浅析源码以解惑 且记录,将来在项目上尽量避免此类问题的出现,大家都知道HashMap为key-value存储,在HashMap中,HashMap本身拥有一个Entry数组,Entry则存有key-value,且对于Hashmap来讲一个key只能对应一个value     首先是put方法          

Java 区块链BLOCKCHAIN中区块BLOCK的hash值的计算

Java 区块链中区块的hash值的计算 计算方法有多种,如,可以直接String拼接,也可以用stringbuffer,或者stringbuilder .这里采用了速度较快的stringbuilder,自己编程的时候可采用stringbuffer.其中index是区块BLOCK的索引,timestamp是区块BLOCK的时间戳,data是区块BLOCK内包含的数据,nonce为该区块的难度系数.总体计算代码如下: /** * 计算hash服务 * @param index 索引 * @para

【二分答案+智障的字符串hash】BZOJ2946-[Poi2000]公共串(Ranklist倒一达成!!!!!)【含hash知识点】

[题目大意] 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. [字符串hash的小笔记] hash[i]=(hash[i-1]*p+idx(s[i]))%mod,idx为映射值,一般a..z映射1..26: 习惯上,p取一个6到8位的素数即可,mod一般取大素数 1e9+7(1000000007)或1e9+9(1000000009). hash[i]=(hash[i-1]*p+idx(s[i]))%mod 表示第 i 个前缀的hash值,是一个hash的前缀和,那么,要求S[l…r]