1. Store some constant nonzero value, say 17, in an int variable called result.
2. For each significant field f in your object (each field taken into account by the
equals method, that is), do the following:
a. Compute an int hash code c for the field:
i. If the field is a boolean, compute (f ? 0 : 1).
ii. If the field is a byte, char, short, or int, compute (int)f.
iii. If the field is a long, compute (int)(f ^ (f >>> 32)).
iv. If the field is a float compute Float.floatToIntBits(f).
v. If the field is a double, compute Double.doubleToLongBits(f), and
then hash the resulting long as in step 2.a.iii.
vi. If the field is an object reference and this class’s equals method
compares the field by recursively invoking equals, recursively
invoke hashCode on the field. If a more complex comparison is
required, compute a “canonical representation” for this field and
invoke hashCode on the canonical representation. If the value of the
field is null, return 0 (or some other constant, but 0 is traditional).
vii. If the field is an array, treat it as if each element were a separate field.
That is, compute a hash code for each significant element by applying
these rules recursively, and combine these values as described in
step 2.b.
b. Combine the hash code c computed in step a into result as follows:
result = 37*result + c;
3. Return result.
4. When you are done writing the hashCode method, ask yourself whether equal
instances have equal hash codes. If not, figure out why and fix the problem.
分享到:
相关推荐
Considering the complex of the data required by Ministry of Transport of P.R.C every period of ten days, I design the following algorithm to generate the data required.
介绍如何在VanetMobiSim中产生NS2能够使用的trace file
How to use SFTP (with client validation - public key authentication) The topic How to use SFTP (with client validation - password authentication) discusses the simplest form of client ...
You will learn about the basic plots, how to customize them, and combine them to make sophisticated figures. Along with basic plots, you will also learn to make professional scientific plots.
Failed to Generate Report(解决方案).md
Failed to generate secure key pair(解决方案).md
Immersive Teleconferencing A New Algorithm to Generate Seamless Panoramic.pdf
#### 标题:Keyboard-and-Console-HOWTO **标题解读:** 此文档主要介绍了Linux操作系统中的键盘和控制台管理的相关知识和技术细节,包括非ASCII字符的使用方法。 #### 描述:This note contains some information ...
Excel Macro to Generate Database Insert Script Excel宏生成数据库插入脚本 对于开发人员来说,一次在数据库中添加/编辑主数据或静态数据一直很麻烦,因为它需要为每个小的文本更改集编写脚本。 对于不了解SQL...
例如,给出的`memory_to_vector`模块就是一个利用generate语句将二维数组转换为一维向量的例子。 总之,`generate`是Verilog编程中一个强大的工具,它提供了在硬件描述语言中创建复杂并行结构和条件实例化的能力,...
How to generate a runnable jar QArt4j is a maven project, run the following command to get a ruunable jar: mvn compile assembly:single This will generate a runnable jar under target/ directory. How to...
《CheckSum_Generate_exe_v7.1536.00.00:MTK平台校验和生成工具详解》 在IT行业中,数据的完整性和安全性至关重要,尤其是在固件更新、软件分发等场景下。为了确保文件在传输过程中没有被篡改或损坏,通常会使用...
you can add & view records. It is automatically generate on EXCEL, MSWORD, NOTEPAD and also to any other office encoder. How to change extension format files LIKE .csv .doc .txt
You will discover how to build an engaging Unreal iOS game, how to generate revenue, and how to optimize game performance using the tools and functionalities the Engine provides. To begin, you will ...
《codegenerate-3.6.1源码解析与二次开发指南》 在IT行业中,源码分析和二次开发是提升软件功能、优化性能的重要手段。本文将深入探讨"codegenerate-3.6.1源码",它是基于Jeecg框架的自动生成代码工具的源代码版本...
在使用QUARTUS II进行FPGA项目开发时,编译过程中可能会遇到“Error: Run Generate Functional Simulation Netlist”的错误提示,这通常是由于缺少仿真网表导致的。在解决这个问题之前,我们首先要理解QUARTUS II的...