==,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 ...
随机推荐
- 为Druid监控配置访问权限(配置访问监控信息的用户与密码)
转: l 为Druid监控配置访问权限(配置访问监控信息的用户与密码) 2014-09-26 09:21:48 来源:renfufei的专栏 收藏 我要投稿 Druid是一 ...
- Unity 着色器基础知识
一.着色器基础知识 着色器通过代码模拟物体表面发生的事情,其实就是GPU中运行的一段代码. 着色器的类型: 顶点着色器.片元着色器.无光照着色器.表面着色器.图像特效着色器.计算着色器. 坐标空间: ...
- C内存分配
calloc和realloc与malloc的区别 calloc和realloc的原型如下: void *calloc ( size_t num_elements, size_t element_siz ...
- CodeForces1065F 树形dp
http://codeforces.com/problemset/problem/1065/F 你有一棵带有n个结点的树,根是结点1.有一个标记,最初在根结点处.你可以将标记移动到其他结点处.假设标记 ...
- 2Servlet笔记
1.静态页面(html) 2.动态 用户可以输入数据,和页面交互. 3 BS和CS的区别 BS :Browser Server(WEB 服务器) CS : Client(qq tcp/ip协议) ...
- Windows 运行库
Microsoft Visual C++ 2005 Redistributable - 8.0.61001http://download.microsoft.com/download/8/B/4/8B ...
- SecureCRT for ubuntu 菜单消失
两种解决方案. 1.先说网上查到的复杂的: 编辑CRT安装目录下的Global.ini 找到 D:"Show Menu Bar"=00000000 改成 D:"Show ...
- zookeeper安装(单机版)
1:查看当前服务器IP: # ifconfig 情况1:直接可以看到自己的IP:192.168.164.130 情况2:看不到自己的IP(但是能看到ifcfg-开头的东西,如:ifcfg-eno167 ...
- 解决Lost connection to MySQL server during query错误方法
昨天使用Navicat for MySQL导入MySQL数据库的时候,出现了一个严重的错误,Lost connection to MySQL server during query,字面意思就是在查询 ...
- bzoj千题计划314:bzoj3238: [Ahoi2013]差异(后缀数组+st表+单调栈)
https://www.lydsy.com/JudgeOnline/problem.php?id=3238 跟 bzoj3879 差不多 #include<cstdio> #include ...