equals与Hashcde的区别

news/2024/10/23 7:24:21/

1、equals与hashCode的区别

equals与hashcode这两个方法都是从Object类中继承过来的。

hashCode():计算出对象实例的哈希地址,并且返回该地址,称哈希函数,又称散列表。

equals():反映的是对象的内存地址或者对象的内容是否相等。

Objeact类实现equals()是对两个对象的地址进行比较(即比较是否相同,也就是内存地址是否相等)。

Object类实现hashCode()方法,其实就是调用C++编写的本地方法,可以理解为产生一个随机数的本地方法。

Java中的两个规则:

1、规则一

如果equals比较的对象的内容相等,那么hashCode一定相等。

2、规则二

如果equals比较的对象内容不相等,那么HashCode也可能相等,也可能不相等。

所以:HashCode如果相同的话,那么对象的值不一定相同。

变量a,b hashCode值相同的原因是,Integer和String类都对hashCode进行重写。

规则三:HashCode如果相等的情况下,equals比较对象不一定相等。(根据规则一、二推出)。

规则四:HashCode如果不相等的情况下,equas比较对象一定不相等(根据规则一、二推出)。

class Dog{private String name;private int age;public Dog(String name, int age) {this.name = name;this.age = age;}
}
public class Test {public static void main(String[] args) {Dog dog1=new Dog("huahua",11);Dog dog2=new Dog("huahua",11);System.out.println("dog1的hashcode:"+dog1.hashCode());System.out.println("dog2的hashcode:"+dog2.hashCode());System.out.println("dog1和dog2的比较结果:"+dog1.equals(dog2));}
}
//运行结果:
//dog1的hashcode:460141958
//dog2的hashcode:1163157884
//dog1和dog2的比较结果:false

我们看见比较对象相同,但是hashcode也不相同,违背了规则一,因为我们没有重写,equals

方法。

class Dog{private String name;private int age;public Dog(String name, int age) {this.name = name;this.age = age;}@Overridepublic boolean equals(Object obj) {Dog dog=(Dog) obj;return this.age== dog.age&&this.name.equals(dog.name);}
}
public class Test {public static void main(String[] args) {Dog dog1=new Dog("huahua",11);Dog dog2=new Dog("huahua",11);System.out.println("dog1的hashcode:"+dog1.hashCode());System.out.println("dog2的hashcode:"+dog2.hashCode());System.out.println("dog1和dog2的比较结果:"+dog1.equals(dog2));}
}
//运行结果:
//dog1的hashcode:460141958
//dog2的hashcode:1163157884
//dog1和dog2的比较结果:true

此时我们可以看到比较对象相同,但是hashcode的值确实不相同的,是因为我们没有重写hashcode。

class Dog{private String name;private int age;public Dog(String name, int age) {this.name = name;this.age = age;}@Overridepublic int hashCode() {return Objects.hash(name,age);}@Overridepublic boolean equals(Object obj) {Dog dog=(Dog) obj;return this.age== dog.age&&this.name.equals(dog.name);}
}
public class Test {public static void main(String[] args) {Dog dog1=new Dog("huahua",11);Dog dog2=new Dog("huahua",11);System.out.println("dog1的hashcode:"+dog1.hashCode());System.out.println("dog2的hashcode:"+dog2.hashCode());System.out.println("dog1和dog2的比较结果:"+dog1.equals(dog2));}
}
//运行结果:
//dog1的hashcode:1253509196
//dog2的hashcode:1253509196
//dog1和dog2的比较结果:true


http://www.ppmy.cn/news/930577.html

相关文章

Unity2D游戏开发基础(四)控制角色——跳跃触地检测

大家好,很高兴和大家分享我们的Unity游戏开发经验, 这是我们的系列视频的第四期视频, 在这期视频中,我们将继续对上一期视频中完成的基本的跳跃控制进行地面检测的补充,保证角色只有在落地后才能开始下一次的跳跃&…

Unity2D游戏开发基础(三)控制角色——跳跃

大家好,很高兴和大家分享我们的Unity游戏开发经验, 这是我们的系列视频的第三期视频, 在这期视频中,我们将在Unity中使用C#脚本来实现角色的跳跃,包括跳跃代码的改进,让角色跳跃更加真实,同时也…

跳跃游戏

给定一个非负整数数组&#xff0c;你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。 代码&#xff1a; class Solution { public:bool canJump(vector<int>& nums) {int k0;for(int i0;i<…

类跑酷游戏“忍者跳跃”的 Cocos Creator实现

​游戏的样子&#xff1f; 游戏玩法&#xff1f; 地面运动时点击屏幕&#xff0c;控制英雄左右跳动&#xff0c;躲避飞镖和柱子&#xff0c;撞到飞镖减血&#xff0c;吃到蛋糕加血&#xff0c;撞到柱子游戏结束。 游戏逻辑&#xff1f; 游戏主要分为开始界面和游戏中界面&…

Android游戏源码——忍者快跑

2019独角兽企业重金招聘Python工程师标准>>> 忍者突袭(Ninja Rush)这是一款非常好玩且耐玩的游戏。游戏中你扮演一个忍者&#xff0c;去消灭敌人&#xff0c;冲向丛林的尽头!   忍者突袭游戏中有&#xff1a;跳跃、飞镖、飞跃、加速等多种动作组合&#xff0c;使游…

IT忍者神龟之vim命令大全

1 Vim的几种模式 正常模式&#xff1a;可以使用快捷键命令&#xff0c;或按:输入命令行。 插入模式&#xff1a;可以输入文本&#xff0c;在正常模式下&#xff0c;按i、a、o等都可以进入插入模式。 可视模式&#xff1a;正常模式下按v可以进入可视模式&#xff0c; 在可视模式…

1.1[真的好玩] C++小游戏·跑酷(俗称忍者必须死单机版)

家人们&#xff0c;C真的好玩系列开坑啦&#xff01; ONE&#xff1a;跑酷 【一个好人写滴】 代码奉上&#xff01;&#xff01;&#xff01; #include<bits/stdc.h> #include<windows.h> #include<stdio.h> #include<conio.h> #include<ti…

CocosCreator实战项目1:忍者跳跳跳

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 摘要正文使用版本游戏截图游戏资源面板脚本关系代码部分 结语 摘要 CocosCreator模仿4399忍者跳跳跳小游戏 原版游戏链接&#xff1a; 忍者跳跳跳 正文 使用版本…