==,hashcde, equals(一)
1、Hash 的属性,
1)bucket 和 list
2、java.lang.object 的 hashcode 和 equal 通过内存地址比较
3、为什么要重写hashcode 和 equals,
1) 实现hashset不可重复的特性(http://www.cnblogs.com/happyPawpaw/p/3744971.html)
2)Hashmap
4、Hashmap的键值如果是类
5、Hashmap的put 和 get 方法
https://blog.csdn.net/lan12334321234/article/details/70048493
(https://blog.csdn.net/VIP_WangSai/article/details/77505517)
6、先判断hashcode, 再判断equals
为什么要这种判断顺序呢,因为要实现的是hash的特性,bucket由hashcode 决定;
hashcode true, equal 不一定 true
equals true, hashcode 一定true
==,hashcde, equals
public class Student{
private int id;
private String name;
public Student(int id, String name){
this.id = id;
this.name = name;
}
}
main
public static void main(String args[]) {
Student student01 = new Student(1,"lee");
Student student02 = new Student(1,"lee");
System.out.print("student01 == student02 :");
if(student01 == student02){
System.out.println("true.");
}else{
System.out.println("false.");
}
System.out.print("student01.hashCode() == student02.hashCode() :");
if(student01.hashCode() == student02.hashCode()){
System.out.println("true.");
}else{
System.out.println("false.");
}
System.out.print("student01.equals(student02) :");
if(student01.equals(student02)){
System.out.println("true.");
}else{
System.out.println("false.");
}
Set<Object> set = new HashSet<Object>();
set.add(student01);
set.add(student02);
System.out.println("HashSet 长度:" + set.size());
}
输出
默认,继承Object
student01 == student02 :false.
student01.hashCode() == student02.hashCode() :false.
student01.equals(student02) :false.
HashSet 长度:2
修改student 的 hashcode类如下
public class StudentModifyHashCode{
private int id;
private String name;
public StudentModifyHashCode(int id, String name){
this.id = id;
this.name = name;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime*result+this.id;
return result;
}
}
输出,可以看到这次hashcode 一样的
修改hashcode
student01 == student02 :false.
student01.hashCode() == student02.hashCode() :true.
student01.equals(student02) :false.
HashSet 长度:2
同时修改student的hashcode 和 equals 方法如下
public class StudentModifyEqualAndHash{
private int id;
private String name;
public StudentModifyEqualAndHash(int id, String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime*result+this.id;
return result;
}
@Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
StudentModifyEqualAndHash other = (StudentModifyEqualAndHash)obj;
if(this.id != other.getId() ){
return false;
}
for(int i=0;i<this.name.length();i++){
if(this.name.charAt(i) != (((StudentModifyEqualAndHash)obj).getName().charAt(i))){
return false;
}
}
return true;
}
}
输出,可以看到hashcode 和 equals 都一样
修改hashcode和equals
student01 == student02 :false.
student01.hashCode() == student02.hashCode() :true.
student01.equals(student02) :true.
HashSet 长度:1
https://blog.csdn.net/lixiaoxiong55/article/details/93376852
hashCode作用: https://blog.csdn.net/qq_38977097/article/details/80834525
==,hashcde, equals(一)的更多相关文章
- equals变量在前面或者在后面有什么区别吗?这是一个坑点
我就不废话那么多,直接上代码: package sf.com.mainTest; public class Test { public static void main(String[] args) ...
- How to implement equals() and hashCode() methods in Java[reproduced]
Part I:equals() (javadoc) must define an equivalence relation (it must be reflexive, symmetric, and ...
- 【特种兵系列】String中的==和equals()
1. 小样示例 public static void main(String[] args) { String a = "a" + "b" + 123; Str ...
- (转)浅谈Java中的equals和==
原文地址: http://www.cnblogs.com/dolphin0520/p/3592500.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new S ...
- 浅谈Java中的equals和==(转)
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...
- List<T>Find方法,FindAll方法,Contains方法,Equals方法
假如传入的T是一个类, List<MessageInfos> MessageInfos = new List<MessageInfos>(); MessageInfos= Me ...
- 让代码重构渐行渐远系列(3)——string.Equals取代直接比较与非比较
重构背景及原因 最近由于项目组的人员在不断扩充,导致项目中代码风格各异,大有百花齐放甚至怒放之势.考虑到团队的生存与发展,经过众人多次舌战之后,最终决定项目组根据业务分成几个小分队,以加强团队管理与提 ...
- [java] 更好的书写equals方法-汇率换算器的实现(4)
[java] 更好的书写equals方法-汇率换算器的实现(4) // */ // ]]> [java] 更好的书写equals方法-汇率换算器的实现(4) Table of Content ...
- Equals和ReferenceEquals
稍微分析下一下两个方法的区别: public static bool Equals(object objA, object objB); public static bool ReferenceEqu ...
随机推荐
- mybatis的一些小知识
<update id="setReaded"> update wa_ent_job_app set IS_READ= 1, READ_TIME=now() where ...
- android studio adb.exe已停止工作(全面成功版 进程的查询和开启)
先输入adb看是否存在. 如果不存在则:在系统path里添加C:\Users\nubia\AppData\Local\Android\sdk\platform-tools 因为这个目录里有adb 或者 ...
- Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)
E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...
- ArrayList、LinkList、Vector的区别
ArrayList.LinkedList和Vector均实现了List接口,均为可伸缩数组(均为可动态改变长度的数组).它们是有序的集合,并且其中的元素允许重复. 从底层实现来看: (1)ArrayL ...
- linux简单优化
1.简单优化 #关闭firewalld,selinux,NetworkManager systemctl(管理服务的命令) stop(关服务) firewalld (服务名称,d是demo的意思) s ...
- 2017-12-14python全栈9期第一天第八节之循环语句while
12,while. while 条件: 循环体 无限循环. 终止循环:1,改变条件,使其不成立. 2,break continue
- SQL语法基础之INSEART语句
SQL语法基础之INSEART语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看INSERT方法的帮助信息 mysql> ? INSERT ...
- MySQL入门介绍(mysql-8.0.13)
MySQL入门介绍(mysql-8.0.13单机部署) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL数据库介绍 1>.MySQL是一种开放源代码的关系型数据库 ...
- Mybatis笔记一:写一个demo
什么是Mybatis? 在Java中,我们连接数据库可以使用最初级的JDBC,但是这样很麻烦,每次都要写好多,所以Mybatis出现了,Mybatis可以帮我们很简单很简单的实现与数据库的读取改写操作 ...
- 4.工厂方法模式(Factory Method)
耦合关系: 动机(Motivation): 在软件系统中,由于需求的变化,"这个对象的具体实现"经常面临着剧烈的变化,但它却有比较稳定的接口. 如何应对这种 ...