重写Euqals & HashCode
2024-09-04 13:37:14
package com.test.collection; import java.util.HashMap;
import java.util.Map; /**
* 重写equals & hashcode
*
* 1.如果两个对象的equals为true ,则hashCode也为true
* 2.重写equals时,必须重写hashCode(保证equals为true ,hashCode也为true)
* @author chenx
*
*/
public class Student { public Student(int id, String name) {
super();
this.id = id;
this.name = name;
} private int id;
private String name;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + 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;
Student other = (Student) obj;
if (id != other.id)
return false;
return true;
}
}
最新文章
- 解决NetBeans运行web项目时出现的“未能正确设置java DB”问题
- android xml 布局错误
- 前端神器Sublime Text3 常用插件&;常用快捷键
- 解析大型.NET ERP系统 代码的坏味道
- CSS里的pointer-events属性
- 如何启动/停止/重启MySQL
- 【读书笔记】读《JavaScript设计模式》之适配器模式
- javascript实例学习之五——瀑布流布局
- json字符串相关转换方法
- Net Core 的公共组件之 Http 请求客户端
- Codeforces Gym 100015F Fighting for Triangles 状压DP
- 栈中的push实现
- html5图片标签与属性
- DDD实践
- JavaScript用法
- 【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp
- c#中快速排序的学习
- 汇编 inc 和 dec 指令
- bash里wget失败
- laravel 5.3 ——路由(资源,别名)