java中hashCode和equals什么关系,hashCode到底怎么用的
2014-11-21 17:00
169 查看
Object类的hashCode的用法:
马克-to-win:hashCode方法主要是Sun编写的一些数据结构比如Hashtable的hash算法中用到。因为hash很快,所以你往Hashtable里放东西的时候,他先比一下,里面有没有现有的东西的hashCode和你一样,如果都不一样,证明是新的,就不再运行equals方法了,直接放进Hashtable里了,很快。如果放的时候,Hashtable里面现有的某东西的hashCode和他一样,他再运行一下equals,如不一样,则证明是新的,可以放入。equals也一样,证明确实是一样的,不让放入Hashtable。另外,Object的hashCode方法(Sun公司编的)是返回对象的内部地址。(以下摘自sun的文档:Asmuch
as is reasonably practical, the hashCode method defined byclass Object does return distinct integers for distinct objects.(This is typically implemented by converting the internal addressof the object into an integer, but this implementation technique isnot
required by the JavaTM programming language.)
例2.1.2.1(hashCode都不一样)
import java.util.*;
class CompanyMark_to_win {
private String name;
CompanyMark_to_win(String name) {
this.name = name;
}
public boolean equals(Object o) {
System.out.println("equals被调用");
if (!(o instanceof CompanyMark_to_win)) return false;
CompanyMark_to_win c = (CompanyMark_to_win) o;//downcast,向下转型
return name.equals(c.name);//这个equals是String的方法
}
public int hashCode() {
System.out.println("hashCode 被调用 "+super.hashCode());
return super.hashCode();
}
}
public class Test {
public static void main(String[] args) {
CompanyMark_to_win c1 = new CompanyMark_to_win("Abc");
。。。。。。。。。。。。。。。。。
详情请进:http://www.mark-to-win.com/JavaBeginner/JavaBeginner3_web.html#hashCode
马克-to-win:hashCode方法主要是Sun编写的一些数据结构比如Hashtable的hash算法中用到。因为hash很快,所以你往Hashtable里放东西的时候,他先比一下,里面有没有现有的东西的hashCode和你一样,如果都不一样,证明是新的,就不再运行equals方法了,直接放进Hashtable里了,很快。如果放的时候,Hashtable里面现有的某东西的hashCode和他一样,他再运行一下equals,如不一样,则证明是新的,可以放入。equals也一样,证明确实是一样的,不让放入Hashtable。另外,Object的hashCode方法(Sun公司编的)是返回对象的内部地址。(以下摘自sun的文档:Asmuch
as is reasonably practical, the hashCode method defined byclass Object does return distinct integers for distinct objects.(This is typically implemented by converting the internal addressof the object into an integer, but this implementation technique isnot
required by the JavaTM programming language.)
例2.1.2.1(hashCode都不一样)
import java.util.*;
class CompanyMark_to_win {
private String name;
CompanyMark_to_win(String name) {
this.name = name;
}
public boolean equals(Object o) {
System.out.println("equals被调用");
if (!(o instanceof CompanyMark_to_win)) return false;
CompanyMark_to_win c = (CompanyMark_to_win) o;//downcast,向下转型
return name.equals(c.name);//这个equals是String的方法
}
public int hashCode() {
System.out.println("hashCode 被调用 "+super.hashCode());
return super.hashCode();
}
}
public class Test {
public static void main(String[] args) {
CompanyMark_to_win c1 = new CompanyMark_to_win("Abc");
。。。。。。。。。。。。。。。。。
详情请进:http://www.mark-to-win.com/JavaBeginner/JavaBeginner3_web.html#hashCode
相关文章推荐
- java中的hashCode()和equals()的关系
- java中hashcode和equals方法的关系
- C# 中的 == 和 equals()有什么区别?/java中==和equals和hashCode的理解
- java 中 equals()和hashCode()的关系
- java-“==”、equals和hashcode有什么区别
- Activity到底是啥?生命周期是怎么回事?xml有什么关系?
- java 中hashcode 与 equals的关系
- 学习笔记-JAVA-考点10-什么情况下需要重写equals和hashcode()两个方法?
- equals()和hashCode()是什么关系
- JAVA - hashcode与equals作用、关系
- java 中hashcode 与 equals的关系
- java基础之问题:请说出hashCode方法、equals方法、HashSet、HashMap之间的关系
- java 中hashcode 与 equals的关系
- Java学习之equals和hashcode的关系
- 深入java的equals方法,怎么比较两个对象的内容是否相等?对象的内容指的是什么?
- java 中hashcode 与 equals的关系
- 【JAVA学习】java中==、equals()、hashCode()都和对象的比较有关,在java中这三者各有什么用处呢,即java中为什么需要设计这三种对象的比较方法呢?
- java 中hashcode 与 equals的关系
- 重写Java 的equals和hashcode方法的时候应该注意什么
- Java中equals和==的区别,hashCode和equals的关系