public class Person
{
private int id;
private String name;
private String password;
private double salary;
private int hashCode;
public Person(int id,String name,String password,double salary) {
this.id = id;
this.name = name;
this.password = password;
this.salary = salary;
}
public boolean equals(Object obj) {
if (this == obj)
{
return true;
}
if (!(obj instanceof Person))
{
return false;
}
Person other = (Person)obj;
if (this.id != other.id)
{
return false;
}
if (!nullSafeEquals(this.name,other.name))
{
return false;
}
if (!nullSafeEquals(this.password,other.password))
{
return false;
}
if (Double.doubleToLongBits(this.salary) != Double.doubleToLongBits(other.salary))
{
return false;
}
return true;
}
private boolean nullSafeEquals(Object obj1,Object obj2) {
return obj1 == null ? obj2 == null : obj1.equals(obj2);
}
public int hashCode() {
if (hashCode == 0)
{
int result = 17;
result = result * 37 + this.id;
result = result * 37 + (this.name == null ? 0 : this.name.hashCode());
result = result * 37 + (this.password == null ? 0 : this.password.hashCode());
long temp = Double.doubleToLongBits(this.salary);
int salaryInt = (int)(temp ^ (temp >>> 32));
result = result * 37 + salaryInt;
hashCode = result;
}
return hashCode;
}
public String toString() {
return super.toString() + ": [" + this.name + "]";
}
}
分享到:
相关推荐
重写equals和hashcode方法,学习和进步
Java容器集合(equals 和 hashCode+基础数据结构+ArrayList+Vector和LinkedList)
本文还介绍了定义对象的相等性、实施equals()和hashCode()的需求、编写自己的equals()和hashCode()方法。通过统一定义equals()和hashCode(),可以提升类作为基于散列的集合中的关键字的使用性。
但是为什么JavaDoc明确的告诉我们, hashCode()和equals()要一起重写呢?原因是因为,在Java自带的容器HashMap和HashSet中, 都需同时要用到对象的hashCode()和equals()方法来进行判断,然后再插入删除元素,这点...
如果一个类的hashCode()方法没有遵循上述要求,那么,当这个类的两个实例对象用equals()方法比较的结果相等时,他们本来应该无法被同时存储进set集合
NULL 博文链接:https://alpha2009.iteye.com/blog/570322
1.概述 2.为什么重写equels方法要重写hashcode方法 3.例子
这里是一个文档,里边讲解了hashCode与equals方法使用,大家要是不明白,可以去看看
在Java中,equals和hashCode方法是Object中提供的两个方法,这两个方法对以后的学习有很大的帮助,本文就深度来去讲解这两个方法。下面小编带大家来一起学习吧
Java重写equals同时需要重写hashCode的代码说明,以及如何重写hashCode方法,此代码演示按照effective java书籍说明的重写思路。代码中演示了使用集合存储对象,并且对象作为key,需重写equals和hashCode.
本篇文章详细介绍了Java中的equals和hashCode方法详解,Object 类是所有类的父类,非常具有实用价值,需要的朋友可以参考下。
今天小编就为大家分享一篇关于关于Object中equals方法和hashCode方法判断的分析,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
本文中详细的阐述了Java中经常遇到的equals、hashcode以及“==”号三者之间的区别
NULL 博文链接:https://jackosn-liao.iteye.com/blog/528757
关于重写equals,hashcode以及compareTo方法!
equals()和hashcode()这两个方法都是从object类中继承过来的。当String 、Math、还有Integer、Double。。。。等这些封装类在使用equals()方法时,已经覆盖了object类的equals()方法.
EqualsVerifier可用于Java单元测试中,以验证是否满足类中equals和hashCode方法的约定。 Maven坐标为: < dependency> < groupId>nl.jqno.equalsverifier</ groupId> < artifactId>equalsverifier</ artifactId> ...
举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character
要注意的是List,Set,Queue继承了Collection接口,...这里想用一个简单的例子展示一下他们的使用,内容包括:List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类,泛型、重写equals、hashCode
jv-equals-hashcode 给定的类—矩形。 覆盖它的方法equals()和hashCode()所以equals()通过其字段(颜色,宽度和长度equals()比较Rectangle对象。 还要确保hashCode()遵循equals和hashcode之间的约定。