两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话应该是对的吧!

yyouyou 2005-11-25 11:48:03
对象值到底指什么?(x.equals(y) == true)应该并不代表对象值相同
class A
{
A(){}
public boolean equals(A a)
{
return true;
}
}
public class EqualsTest
{
public static void main(String argv[])
{
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
System.out.println(a1.hashCode());
System.out.println(a2.hashCode());
}
}

...全文
2857 57 打赏 收藏 转发到动态 举报
写回复
用AI写文章
57 条回复
切换为时间正序
请发表友善的回复…
发表回复
liu_you 2005-11-29
  • 打赏
  • 举报
回复
API的规范中有这样一句:
Note that it is generally necessary to override the hashCode method whenever this method is overridden,so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.



public boolean equals(Object obj)Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:

It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.


Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.
yyouyou 2005-11-29
  • 打赏
  • 举报
回复
谢谢大家!
结帖!
jimshen 2005-11-25
  • 打赏
  • 举报
回复
对的,相等性比较和hashCode的计算依据可以由类的编写者确定
vvpang 2005-11-25
  • 打赏
  • 举报
回复
对的。。
成富 2005-11-25
  • 打赏
  • 举报
回复
如果两个对象是equal的话,则hashCode必须相同。
而不相等的两个对象的hashCode不并不相同,但是如果不相同可以提高hash table的效率。

---《Effective Java》 P36
jimshen 2005-11-25
  • 打赏
  • 举报
回复
对不起,输入错误。

总结一下,equals()是对象相等性比较,hashCode()是计算对象的散列值,当然他们的依据是对象的属性。
jimshen 2005-11-25
  • 打赏
  • 举报
回复
总结一下,equals()是对象相等性比较,hashCode()是计算对象的散列值,当然他们的语句是对象的属性。

对于equals,一般我们认为两个对象同类型并且所有属性相等的时候才是相等的,在类中必须改写equals,因为Object类中的equals只是判断两个引用变量是否引用同一对象,如果不是引用同一对象,即使两个对象的内容完全相同,也会返回false。当然,在类中改写这个equals时,你也可以只对部分属性进行比较,只要这些属性相同就认为对象是相等的。

对于hashCode,只要是用在和哈希运算有关的地方,前面很多兄弟都提到了,和equals一样,在你的类中也应该改写。当然如果两个对象是完全相同的,那么他们的hashCode当然也是一样的,但是象前面所述,规则可以由你自己来定义,因此两者之间并没有什么必然的联系。

当然,大多数情况下我们还是根据所有的属性来计算hashCode和进行相等性比较。
kingofhawks 2005-11-25
  • 打赏
  • 举报
回复
org.apache.commons.collection里边应该也有类似hashmap的东东吧,也是只有在用到这些东西时才需要实现hashCode()么?没用过,请指教.
treeroot 2005-11-25
  • 打赏
  • 举报
回复
哈希表虽然有各种实现,但是本质上是一样的,都是基于哈希函数
shine333 2005-11-25
  • 打赏
  • 举报
回复
我说的是“不是HashMap/Hashtable”,我没有讲数据结构中的哈希表

而且为什么一定要从JDK里面找?org.apache.commons.collection就不错:P
treeroot 2005-11-25
  • 打赏
  • 举报
回复
当然HashSet本质上是HashMap
LinkedHashSet本质上是LinkedHashMap
treeroot 2005-11-25
  • 打赏
  • 举报
回复
事实上就是哈希表,如果它使用了hashCode其实他就是哈希表的实现

jdk中包括 HashMap,LinkedHashMap,IdentityHashMap,Hashtable

楼上还能列举其他情况?
shine333 2005-11-25
  • 打赏
  • 举报
回复
基本上是这样,不过更准确的说法,并不一定是HashMap/table,

而是那些根据上面这个规范,进行hash操作的地方

kingofhawks 2005-11-25
  • 打赏
  • 举报
回复
完全同意treeroot(旗鲁特) ( ) 的观点.请看Sun JDK 文档是怎么说的吧.
The general contract of hashCode is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
实际上,如果我们的对象并不会放到HashMap中去的话,根本就不需要实现hashCode.是不是这么理解呢?
kingofhawks 2005-11-25
  • 打赏
  • 举报
回复
treeroot(旗鲁特) ( )
equals()是为hashCode不同而设定的!!!

又是谬论!
只有使用哈希表是才使用hashCode,equals在任何地方都会用到.
如果你需要使用equals方法或者任何容器,就不要忽略它的存在.
只有你需要使用哈希表时你才需要关注hashCode,而且一般要足够离散!
---------------正解.好多时候我们实现equals的时候都可以不用管hashCode.
treeroot 2005-11-25
  • 打赏
  • 举报
回复
潜力贴
shine333 2005-11-25
  • 打赏
  • 举报
回复
又来了一个toString(),这方面不想回答。估计您从来没有自己override过。

话又说回来,equals true/false,而hashCode理论上需要32-bit内平均分布,请问怎么依赖一个只有两种可能的事物,得出一个有2^32种可能的事物
shine333 2005-11-25
  • 打赏
  • 举报
回复
但使用容器时,仍然是没有强制性,因为容器都是接口,实现方式千差万别,

连java.util.Map的class javadoc的最后都说

More generally, implementations of the various Collections Framework interfaces are free to take advantage of the specified behavior of underlying Object methods wherever the implementor deems it appropriate.

也就是说,即使容器,实现的时候,也可以根本不用hashCode或者equals,甚至可能直接用==

当然,这个情况很少会发生。所以,我也不再谈强制性的问题。

关于“equals()是为hashCode相同而设定的”,此乃大谬也。

我也不说两者之间互不隶属(不光语法强制无关,而且是在逻辑上,规范上)

宇宙之中,各种事物何其之多,各种情况不可计数,岂是一个小小的32bit int可以区分?!难道因为hashCode碰巧相同,equals也要为了hashCode相同而忍气吞声?
I_Believe_ 2005-11-25
  • 打赏
  • 举报
回复
不好意思刚才掉线了!!发完这贴真下了,有事!!
TO: shine333(enihs) ,从语法角度你可能是指HASHCODE()与TOSTRING()这样才算有强制关系的吧!!但是equals()与hashCode在使用某些容器时确实有有强制性!说他们分不开不过分,容器很多地方用得到!
TO treeroot(旗鲁特):你钻牛角尖了
ericbu 2005-11-25
  • 打赏
  • 举报
回复
好强的贴!
Mark!
加载更多回复(37)
​ 博主介绍:✌在职Java研发工程师、专注于程序设计、源码分享、技术交流、专注于Java技术领域和毕业设计✌项目名称基于Web的酒店客房管理系统的设计与实现系统说明3.2.1  用户模块功能分析前台模块应主要包括用户登录模块、用户注册模块、查看客房信息模块、客房预定 模块、用户留言模块、充模块和个人信息维护模块。用户登录模块:用户通过输入注册的的账号和密码,然后进行身份验证,匹配成功 后实现登录功能。用户注册模块:用户输入合法的账号和密码可以实现注册功能。 查看客房信息模块:用户可以通过首页查看客房的价格,图片,详情等信息,从而   可以选择想要预定的房间。 客房预定模块:用户选择自己想要预定的客房后,可以通过输入相关信息进行订房操作。用户留言模块:用户可以向管理员发送留言。 充模块:用户可以通过添加银行卡再为自己进行充操作。 个人信息维护模块:用户可以修改自己的姓名、密码、身份证号等信息,还可以查看自己的订单。3.2.2  管理员模块功能分析对于管理员而言,一个好的管理系统总是能让酒店的管理工作事半功倍[7]。管理员 能够通过这个系统对自己的酒店情况一目了然,应该包括客户留言模块、客房管理模 块、订房信息管理模块、入住信息管理模块、统计分析模块、酒店新闻管理模块、会 员信息管理模块、员工信息管理模块、系统用户管理模块、个人信息维护模块。客户留言模块:管理员可以查看并回复用户的留言。客房管理模块:管理员可以管理客房信息,可以添加新的客房,删除已经停用的客房信息,还可以修改现有的客房信息。订房信息管理模块:管理员可以处理用户的订房请求,为用户办理入住手续。入住信息管理模块:对于没有注册的线下客户,也可以办理入住手续。统计分析模块:可以对酒店所有的入住信息进行记录和总结分析。酒店新闻管理模块:管理员可以更新网站上的新闻公告,展示图片等信息。会员信息管理模块:管理员可以查看到所有的注册会员信息,可以对会员信息进行删除,修改,添加操作。员工信息管理模块:管理员可以查看到自己公司所有的员工信息,而且还可以对员工的相关信息进行管理。系统用户管理模块:管理员可以查看到所有的系统管理员信息并对管理员信息进行管理。个人信息维护模块:管理员可以更改自己的登录密码或者是姓名、性别、手机号等 相关个人信息。​编辑 环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本;6.是否Maven项目:否;技术栈1. 后端:Spring+SpringMVC+Mybatis2. 前端:JSP+CSS+JavaScript+jQuery使用说明1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;3. 将项目中springmvc-servlet.xml配置文件中的数据库配置改为自己的配置;4. 运行项目,在浏览器中输入http://localhost:8080/ 登录运行截图​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑 用户管理控制层:package com.houserss.controller;import javax.servlet.http.HttpSession;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.houserss.common.Const;import com.houserss.common.Const.Role;import com.houserss.common.ServerResponse;import com.houserss.pojo.User;import com.houserss.service.IUserService;import com.houserss.service.impl.UserServiceImpl;import com.houserss.util.MD5Util;import com.houserss.util.TimeUtils;import com.houserss.vo.DeleteHouseVo;import com.houserss.vo.PageInfoVo;/** * Created by admin */@Controller@RequestMapping(/user/)public class UserController if (ip != null && ip.length() > 0) String[] ips = ip.split(,);for (int i = 0; i  

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧