- 浏览: 131606 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
qq466862016:
不错的文章
JDK动态代理与CGLIB代理的对比 -
jinxiongyi:
你好,jpedal pdf转换图片的 画质,怎么提高。。我转 ...
介绍几款PDF转图片的开源工具 -
qqdwll:
转图片消耗的内存还是不小。 有时间得找找有没有更好的办法, 把 ...
介绍几款PDF转图片的开源工具 -
xiaoyao3857:
Thanks for your work!It's help ...
Keeping Eclipse running clean (转载) -
iceside:
图片讲解非常详细,说清了引用复制是怎么回事
Java 值传递的终极解释
原文 http://www.javapractices.com/topic/TopicAction.do;jsessionid=8D0BB2D9061334756DC17A10A4FB5F71?Id=28
Implementing hashCode :
* if a class overrides equals, it must override hashCode
* when they are both overridden, equals and hashCode must use the same set of fields
* if two objects are equal, then their hashCode values must be equal as well
* if the object is immutable, then hashCode is a candidate for caching and lazy initialization
It is a popular misconception that hashCode provides a unique identifier for an object. It does not.
Example
The following utility class allows simple construction of an effective hashCode method. It is based on the recommendations of Effective Java, by Joshua Bloch.
Here is an example of its use. When debugging statements are uncommented in HashCodeUtil, the reuse of the boolean, char, int and long versions of hash is demonstrated :
boolean...
char...
int...
long...
long...
int...
int...
int...
int...
int...
hashCode value: -608077094
Implementing hashCode :
* if a class overrides equals, it must override hashCode
* when they are both overridden, equals and hashCode must use the same set of fields
* if two objects are equal, then their hashCode values must be equal as well
* if the object is immutable, then hashCode is a candidate for caching and lazy initialization
It is a popular misconception that hashCode provides a unique identifier for an object. It does not.
Example
The following utility class allows simple construction of an effective hashCode method. It is based on the recommendations of Effective Java, by Joshua Bloch.
import java.lang.reflect.Array; /** * Collected methods which allow easy implementation of <code>hashCode</code>. * * Example use case: * <pre> * public int hashCode(){ * int result = HashCodeUtil.SEED; * //collect the contributions of various fields * result = HashCodeUtil.hash(result, fPrimitive); * result = HashCodeUtil.hash(result, fObject); * result = HashCodeUtil.hash(result, fArray); * return result; * } * </pre> */ public final class HashCodeUtil { /** * An initial value for a <code>hashCode</code>, to which is added contributions * from fields. Using a non-zero value decreases collisons of <code>hashCode</code> * values. */ public static final int SEED = 23; /** * booleans. */ public static int hash( int aSeed, boolean aBoolean ) { System.out.println("boolean..."); return firstTerm( aSeed ) + ( aBoolean ? 1 : 0 ); } /** * chars. */ public static int hash( int aSeed, char aChar ) { System.out.println("char..."); return firstTerm( aSeed ) + (int)aChar; } /** * ints. */ public static int hash( int aSeed , int aInt ) { /* * Implementation Note * Note that byte and short are handled by this method, through * implicit conversion. */ System.out.println("int..."); return firstTerm( aSeed ) + aInt; } /** * longs. */ public static int hash( int aSeed , long aLong ) { System.out.println("long..."); return firstTerm(aSeed) + (int)( aLong ^ (aLong >>> 32) ); } /** * floats. */ public static int hash( int aSeed , float aFloat ) { return hash( aSeed, Float.floatToIntBits(aFloat) ); } /** * doubles. */ public static int hash( int aSeed , double aDouble ) { return hash( aSeed, Double.doubleToLongBits(aDouble) ); } /** * <code>aObject</code> is a possibly-null object field, and possibly an array. * * If <code>aObject</code> is an array, then each element may be a primitive * or a possibly-null object. */ public static int hash( int aSeed , Object aObject ) { int result = aSeed; if ( aObject == null) { result = hash(result, 0); } else if ( ! isArray(aObject) ) { result = hash(result, aObject.hashCode()); } else { int length = Array.getLength(aObject); for ( int idx = 0; idx < length; ++idx ) { Object item = Array.get(aObject, idx); //recursive call! result = hash(result, item); } } return result; } /// PRIVATE /// private static final int fODD_PRIME_NUMBER = 37; private static int firstTerm( int aSeed ){ return fODD_PRIME_NUMBER * aSeed; } private static boolean isArray(Object aObject){ return aObject.getClass().isArray(); } }
Here is an example of its use. When debugging statements are uncommented in HashCodeUtil, the reuse of the boolean, char, int and long versions of hash is demonstrated :
boolean...
char...
int...
long...
long...
int...
int...
int...
int...
int...
hashCode value: -608077094
import java.util.*; public final class ApartmentBuilding { public ApartmentBuilding ( boolean aIsDecrepit, char aRating, int aNumApartments, long aNumTenants, double aPowerUsage, float aWaterUsage, byte aNumFloors, String aName, List aOptions, Date[] aMaintenanceChecks ){ fIsDecrepit = aIsDecrepit; fRating = aRating; fNumApartments = aNumApartments; fNumTenants = aNumTenants; fPowerUsage = aPowerUsage; fWaterUsage = aWaterUsage; fNumFloors = aNumFloors; fName = aName; fOptions = aOptions; fMaintenanceChecks = aMaintenanceChecks; } @Override public boolean equals(Object that) { if ( this == that ) return true; if ( !(that instanceof ApartmentBuilding) ) return false; ApartmentBuilding thatBuilding = (ApartmentBuilding)that; return hasEqualState(thatBuilding); } @Override public int hashCode() { //this style of lazy initialization is //suitable only if the object is immutable if ( fHashCode == 0) { int result = HashCodeUtil.SEED; result = HashCodeUtil.hash( result, fIsDecrepit ); result = HashCodeUtil.hash( result, fRating ); result = HashCodeUtil.hash( result, fNumApartments ); result = HashCodeUtil.hash( result, fNumTenants ); result = HashCodeUtil.hash( result, fPowerUsage ); result = HashCodeUtil.hash( result, fWaterUsage ); result = HashCodeUtil.hash( result, fNumFloors ); result = HashCodeUtil.hash( result, fName ); result = HashCodeUtil.hash( result, fOptions ); result = HashCodeUtil.hash( result, fMaintenanceChecks ); fHashCode = result; } return fHashCode; } //..other methods elided // PRIVATE //// /** * The following fields are chosen to exercise most of the different * cases. */ private boolean fIsDecrepit; private char fRating; private int fNumApartments; private long fNumTenants; private double fPowerUsage; private float fWaterUsage; private byte fNumFloors; private String fName; //possibly null, say private List fOptions; //never null private Date[] fMaintenanceChecks; //never null private int fHashCode; /** * Here, for two ApartmentBuildings to be equal, all fields must be equal. */ private boolean hasEqualState( ApartmentBuilding that ) { //note the different treatment for possibly-null fields return ( this.fName==null ? that.fName==null : this.fName.equals(that.fName) ) && ( this.fIsDecrepit == that.fIsDecrepit )&& ( this.fRating == that.fRating )&& ( this.fNumApartments == that.fNumApartments ) && ( this.fNumTenants == that.fNumTenants ) && ( this.fPowerUsage == that.fPowerUsage ) && ( this.fWaterUsage == that.fWaterUsage ) && ( this.fNumFloors == that.fNumFloors ) && ( this.fOptions.equals(that.fOptions) )&& ( Arrays.equals(this.fMaintenanceChecks, that.fMaintenanceChecks) ); } /** * Exercise hashcode. */ public static void main (String [] aArguments) { List options = new ArrayList(); options.add("pool"); Date[] maintenanceDates = new Date[1]; maintenanceDates[0] = new Date(); byte numFloors = 8; ApartmentBuilding building = new ApartmentBuilding ( false, 'B', 12, 396L, 5.2, 6.3f, numFloors, "Palisades", options, maintenanceDates ); System.out.println("hashCode value: " + building.hashCode()); } }
发表评论
-
介绍几款PDF转图片的开源工具
2011-09-09 00:40 4493最近项目中有个需求需要把PDF转成一张图。经过调查,有 ... -
jadclipse(反编译Eclipse插件)
2011-07-19 19:13 1620Jad Java decompiler plugin for ... -
Java开发时候的内存溢出
2011-07-13 17:33 1159这里以tomcat环境为例, ... -
class loader
2011-07-08 17:23 0Because Class.getResource() eve ... -
Jakarta-Common-BeanUtils使用笔记
2011-07-06 16:55 1302原文转发http://blog.csdn.net/fa ... -
基于MVC模式Struts框架研究
2011-04-13 20:02 1299不做web开发多年了, 可偶尔去面试的时候, 还是 ... -
Java反射与动态代理
2011-04-13 15:08 938这篇文章是 成富 先生在InfoQ上Java 深度历险系列的一 ... -
Java枚举类型
2011-04-04 19:50 754Tiger中的一个重要新特性是枚举构造,它是一种新的Java枚 ... -
Java 值传递的终极解释
2011-03-21 22:49 1940对于Java的值传递, 你真的了解么? Ja ... -
六种异常处理的陋习
2011-03-20 03:21 732你觉得自己是一个Java专 ... -
数组初始化
2011-03-20 02:40 835数组初始化,你觉得简单吗? a.如果你觉得简单,那请看下面的 ... -
Java 中 immutable class 以及怎样实现immutable 类
2011-03-11 16:47 1330原文 http://www.javapractices.com ... -
Java 内部类介绍
2011-02-16 17:14 948转载: http://zhidao.baidu.com/que ... -
Java 中的Clone 学习总结
2011-01-25 18:22 26971. 一个类需要实现clone. 一个最佳实践是它需要实现 C ... -
java 通过流, nio 移动文件或者文件夹
2011-01-04 17:54 1814我们用例子说明java怎样通过不同的方式移动文件或文件夹。 ... -
转 深入探讨SOAP、RPC和RMI
2010-12-17 00:34 1009这篇文章是从网上转下来的。 原文应该是写于2001年。 10 ... -
java 6 中的性能优化
2010-12-07 15:30 1408文章转载自: http://www ... -
创建强健,稳定的 JMS 系统
2010-12-07 15:21 950The most reliable way to produc ... -
Java Modifier Summary
2010-11-12 15:10 844<tbody> <tr> ... -
[ZT] 怎样停止一个线程或者任务
2010-11-12 15:08 962source file http://forward.com. ...
相关推荐
主要给大家介绍了关于Java中HashCode方法的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
在java中一个hashCode算法,...按下Ctrl键点击hashCode方法名跟进去看了下其算法,发现是很简单的几句代码,如下所示: 代码如下: public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val
哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率。在Java的Object类中有一个方法: public native int hashCode();...在Java中也一样,hashCode方法的主要作用是为了配合基于
hashCode()和equals()定义在Object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法。 使用hashCode()和equals() hashCode()方法被用来获取给定对象的整数。这个整数被用来确定对象被...
如何正确实现Java中的HashCode共6页.pdf.zip
总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set。前者集合内的元素是有序的,元素可以重复;后者元素无序,但元素不可重复
哈希生成器js 一个Vue.js应用程序,其中包含Java中String类的hashCode方法的javascript实现构建设置# install dependenciesnpm install# serve with hot reload at localhost:8080npm run dev# build for production...
面试时经常会问起字符串比较相关的问题,比如:字符串比较时用的什么方法,内部实现如何?hashcode的作用,以及重写equal方法,为什么要重写hashcode方法?以下就为大家解答,需要的朋友可以参考下
Java语言提供了专门机制以解决这种冲突,有效避免了同一个数据对象被多个线程同时访问。 需要明确的几个问题: synchronized关键字可以作为函数的修饰符,也可作为函数内的语句,也就是平时说的同步方法和同步语句...
网上找了好一轮,找到个比较像样而且足够短的 hashCode 实现,是从 Java 的 hashCode 中借鉴而得的。原理见 Java hashCode() ,也可以跟这里的 Java String 的源码 参照对比一下
Java集合中有两类,一类是List,一类是Set他们之间的区别就在于List集合中的元素师有序的,且可以重复,而Set集合中元素是无序不可重复的。对于List好处理,但是对于Set而言我们要如何来保证元素不重复呢?通过迭代...
主要介绍了Java编程实现用hash方法切割文件,简单介绍了hash的概念,然后分享了使用方法示例,具有一定借鉴价值,需要的朋友可以了解下。
本文档详细介绍了set接口为什么会用到hashCode和equals方法以及这两个方法的一些探讨 set不同的实现类用到的这两个方法也不同
JVM(Java Virtual Mechinal),Java虚拟机,是JRE的一部分,它是整个java实现跨平台的最核心的部分,负责运行字节码文件。 二.hashCode()与equals()之间的关系 在lawa中,每个对象都可以调用自己的hashCode)方法...
Java中的equals方法和hashCode方法有什么关系? Java中什么是重载【Overloading】?什么是覆盖【Overriding】?它们有什么区别? Java中什么是多态?如何实现多态? Java中什么是接口?它有什么作用? Java中什么是...
知道如何在特定的条件下选择适合的集合类/接口。 能正确地实现hashcode方法。
第9章 常用类 4课时 理解Object类及其常用方法equals,hashCode和finalize等。 能够使用String,StringBuffer,StringBuilder类创建字符串对象和使用其方法,分辨不同类之间的区别。 ...