hashCode() and equals() Example
来源:互联网 发布:实用的礼物有哪些知乎 编辑:程序博客网 时间:2024/06/06 16:27
Let's take the hashCode() and equals() of java.lang.String as an example.
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0
here is the hashCode():
public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
0 0
- hashCode() and equals() Example
- hashCode() and equals()
- equals() and HashCode()
- hashcode and equals
- hashCode and equals
- hashCode and equals
- equals and hashcode
- Equals and HashCode in Hibernate
- override equals() and hashCode() methods
- Java compareTo, equals and hashcode
- Java equals() and hashCode() Contract
- Java equals() and hashCode() Contract
- Defining hashCode() and equals() effectively and correctly
- Defining hashCode() and equals() effectively and correctly
- 重写hashcode() AND equals()的作用
- Container Class and its equals(), hashCode() methods
- Overriding toString(), hashCode() and equals() Methods
- equals and hashcode method in hibernate framework
- linux下为线程命名
- 归并排序JAVA实现
- cocos2d-x - CCProgressTimer详解
- 树结构
- Android设备管理器漏洞
- hashCode() and equals() Example
- 【C解析之八】 动态内存管理
- DMA方式
- 版本信息 ios
- 3月21日工作总结
- 数据中心IT设备的能量消耗
- Android运行出现“java.io.IOException: 您的主机中的软件放弃了一个已建立的连接。”
- 续:Java Tomcat 中调用.net DLL的方法 导入文件gacutil 和 regasm
- MOS:Master Note for Real Application Clusters (RAC) Oracle Clusterware and Oracle Grid Infrastructur