重写equals和hashCode

时间:2023-02-24 12:48:13
equals()方法
1. 自反性:A.equals(A)要返回true.

2. 对称性:如果A.equals(B)返回true, 则B.equals(A)也要返回true.

3. 传递性:如果A.equals(B)为true, B.equals(C)为true, 则A.equals(C)也要为true. 说白了就是 A = B , B = C , 那么A = C.

4. 一致性:只要A,B对象的状态没有改变,A.equals(B)必须始终返回true.

5. A.equals(null) 要返回false.
//测试

public class Person {
private int age;
private String name;

  
@Override
public boolean equals(Object obj){
if(this == obj){
return true;
}else if(!(obj instanceof Person)){
return false;
}else{
Person p = (Person) obj;
return p.name.equals(name) && p.age == age;
}
} @Override
public int hashCode(){
int result = ;
result = result * + age;
result = result * + name.hashCode();
return result;
} public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
//验证,如果不重写hashCode(),输出false;
   public static void main(String[] args) {
Person p = new Person();
p.setAge(3);
p.setName("ff");
Person p1 = new Person();
p1.setAge(3);
p1.setName("ff");
Set<Person> set = new HashSet<Person>();
set.add(p);
System.out.println(set.contains(p1));
}
 

重写equals和hashCode的更多相关文章

  1. Java中&equals;&equals;、equals、hashcode的区别与重写equals以及hashcode方法实例&lpar;转&rpar;

    Java中==.equals.hashcode的区别与重写equals以及hashcode方法实例  原文地址:http://www.cnblogs.com/luankun0214/p/4421770 ...

  2. 为什么要重写equals和hashcode方法

    equals hashcode  当新建一个java类时,需要重写equals和hashcode方法,大家都知道!但是,为什么要重写呢? 需要保证对象调用equals方法为true时,hashcode ...

  3. Java中equals&lpar;&rpar;和hashCode&lpar;&rpar;的关系以及重写equals&lpar;&rpar;和hashCode&lpar;&rpar;的重要性

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6580647.html  一:关系 如果两个对象相等(equal),它们的hashcode一定相同: 如果两个对 ...

  4. 如何正确的重写equals&lpar;&rpar; 和 hashCode&lpar;&rpar;方法

    比较两个Java对象时, 我们需要覆盖equals和  hashCode. public class User{ private String name; private int age; priva ...

  5. 为什么要同时重写equals和hashcode

    原文地址https://blog.csdn.net/tiantiandjava/article/details/46988461 原文地址https://blog.csdn.net/lijiecao0 ...

  6. Java&colon;重写equals&lpar;&rpar;和hashCode&lpar;&rpar;

    Java:重写equals()和hashCode() 1.何时需要重写equals() 当一个类有自己特有的“逻辑相等”概念(不同于对象身份的概念). 2.设计equals() [1]使用instan ...

  7. 【转】Java中&equals;&equals;、equals、hashcode的区别与重写equals以及hashcode方法实例

    原文地址:http://www.cnblogs.com/luankun0214/p/4421770.html 感谢网友的分享,记录下来只为学习. 1.重写equals方法实例   部分代码参考http ...

  8. 重写equals&lpar;&rpar;与hashCode&lpar;&rpar;方法

    出自:http://blog.csdn.net/renfufei/article/details/16339351 Java语言是完全面向对象的,在java中,所有的对象都是继承于Object类.Oj ...

  9. 为什么要重写equals和hashCode

    1.重写equals方法时需要重写hashCode方法,主要是针对Map.Set等集合类型的使用: a: Map.Set等集合类型存放的对象必须是唯一的: b: 集合类判断两个对象是否相等,是先判断e ...

  10. Java重写equals&lpar;&rpar;和hashCode&lpar;&rpar;

    1.何时需要重写equals() 当一个类有自己特有的 ”逻辑相等”概念(不同于对象身份的概念). 2.设计equals() [1]使用instanceof操作符检查 ”实参是否为正确的类型”. [2 ...

随机推荐

  1. ThinkPHP 3&period;2&period;3 使用 Swift Mailer 邮件系统发送邮件

    SwiftMailer 下载地址:https://github.com/swiftmailer/swiftmailer 版本:swiftmailer-5.x 把压缩包解压到 /ThinkPHP/Lib ...

  2. 【POJ】2234 Matches Game(博弈论)

    http://poj.org/problem?id=2234 博弈论真是博大精深orz 首先我们仔细分析很容易分析出来,当只有一堆的时候,先手必胜:两堆并且相同的时候,先手必败,反之必胜. 根据博弈论 ...

  3. 正则表达式:根据逗号解析CSV并忽略引号内的逗号

    需求:解析CSV文件并忽略引号内的逗号 解决方案: public static void main(String[] args) { String s = "a,b,c,\"1,0 ...

  4. TableView 多余分割线的处理

    方法一,以下两个方法的实现 - (void)viewDidLoad { [super viewDidLoad]; self.tableView.tableFooterView = [[UIView a ...

  5. service层报错找不到方法Invalid bound statement &lpar;not found&rpar;

    报错信息如下 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.imooc.se ...

  6. 安装ODBC前需要安装Visual C&plus;&plus;

    https://mariadb.com/resources/blog/resolving-error-1918-system-error-code-126-when-installing-mysql- ...

  7. &lbrack;LeetCode&rsqb; 422&period; Valid Word Square&lowbar;Easy

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  8. POI导出

    public void export(){try {String columns = getPara("nameArray");List<Record> list = ...

  9. Python&lpar;简单计算器&rpar;

    参考:https://www.cnblogs.com/alex3714/articles/5169958.html import re ret = re.search('\([^()]+\)','(1 ...

  10. CentOS压力测试工具Tsung安装和图形报表生成Tsung安装配置

    Tsung 是一个压力测试工具,可以测试包括HTTP, WebDAV, PostgreSQL, MySQL, LDAP, and XMPP/Jabber等服务器.针对 HTTP 测试,Tsung 支持 ...