Java重写类的HashCode
程序员文章站
2024-03-22 17:06:58
...
我们知道,每次创建一个对象,都会生成 一个新的HashCode,但是在某种情况下,我们希望同一个类的对象的HashCode相同,可以在类的下面写上如下代码即可:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Products other = (Products) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
举一个例子:
package com.aynu.bookstore.beans;
import java.io.Serializable;
public class Products implements Serializable{
private String id;
private String name;
private Double price;
private String category;
private Integer pnum;
private String imgurl;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Integer getPnum() {
return pnum;
}
public void setPnum(Integer pnum) {
this.pnum = pnum;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Products(String id, String name, Double price, String category, Integer pnum, String imgurl,
String description) {
super();
this.id = id;
this.name = name;
this.price = price;
this.category = category;
this.pnum = pnum;
this.imgurl = imgurl;
this.description = description;
}
public Products() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Products [id=" + id + ", name=" + name + ", price=" + price + ", category=" + category + ", pnum="
+ pnum + ", imgurl=" + imgurl + ", description=" + description + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Products other = (Products) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
推荐阅读
-
Java重写类的HashCode
-
关于Java中String类的相关方法
-
抽象类和接口的区别,使用场景 博客分类: java java抽象类接口
-
匿名内部类及Threadlocal 类的使用 博客分类: java java类
-
(转)关于两个jar包中存在包名和类名都完全相同的问题 博客分类: Java javajareclipse
-
Java自学之路-Java基础教程-30:Java Web工程的JSP与初识Servlet类
-
最近正在重写我自己的数据库框架 博客分类: Java 框架SQLExcelJDBC
-
最近正在重写我自己的数据库框架 博客分类: Java 框架SQLExcelJDBC
-
Java多态的理解--父类引用指向子类的对象
-
【SpringBoot动态加载Spring容器的类】 博客分类: 编程语言--JAVA之SpringSpring-Boot