系统相关
首页 > 系统相关> > 内存泄漏避雷!你真的了解重写equals()和hashcode()方法的原因吗?

内存泄漏避雷!你真的了解重写equals()和hashcode()方法的原因吗?

作者:互联网

基本概念

Object源码

public native int hashCode();

重写equals

public class Student {
	/** 姓名 */
	private String name;
	/** 性别 */
	private String sex;
	/** 年龄 */
	private String age;
	/** 体重 */
	private float weight;
	/** 地址 */
	private String addr;

	/*
	 * 重写equals()方法
	 */
	@Override
	public boolean equals(Object obj) {
		// instanceof已经处理了obj == null的情况
	  	if (! (Object instanceof Student)) {
	  		
	  		return false;
	  	}
	  	Student stuObj = (Student) obj;
	  	// 地址相等
	  	if (this == stuObj) {
	  		return true;
	  	}
	  	// 如果对象的姓名,年龄,性别相等.则两个对象相等
	  	if (stuObj.name.equals(this.name) && stuObj.sex.equals(this.sex) && stuObj.age.equals(this.age)) {
	  		return true;
	  	} else {
	  		return false;
	  	}
	 }

	 public String getName() {
	  	return name;
	 }
	 public void setName(String name) {
	  	this.name = name;
	 }
	 public String getSex() {
	  	return sex;
	 }
	 public void setSex(String sex) {
	  	this.sex = sex;
	 }
	  public String getAge() {
	  	return age;
	 }
	 public void setAge(String age) {
	  	this.age = age;
	 }
	 public String getWeight() {
	  	return weight;
	 }
	 public void setName(String weight) {
	  	this.weight = weight;
	 }
	 public String getAddr() {
	  	return addr;
	 }
	 public void setAddr(String addr) {
	  	this.addr = addr;
	 }
}
public static void main(String[] args) {
	Student s1 = new Student();
	s1.setAddr("earth");
	s1.setAge("20");
	s1.setName("Tom");
	s1.setSex("Male");
	s1.setWeight(60f);

	Student s2 = new Student();
	s2.setAddr("Mars");
	s2.setAge("20");
	s2.setName("Tom");
	s2.setSex("Male");
	s2.setWeight(70f);

	if (s1.equals(s2)) {
		System.out.println("s1 == s2");
	} else {
		System.out.println("s1 != s2");
	}
}

重写hashCode

```java
public class Student {
	/** 姓名 */
	private String name;
	/** 性别 */
	private String sex;
	/** 年龄 */
	private String age;
	/** 体重 */
	private float weight;
	/** 地址 */
	private String addr;

	/*
	 * 重写hashCode()方法
	 */
	@Override
	public int hashCode() {
	 	int result = name.hashCode();
	 	result = 17 * result + sex.hashCode();
	 	result = 17 * result + age.hashCode();
	 	return result;
	 }
	 
	 /*
	  * 重写equals()方法
	  */
	 @Override
	 public boolean equals(Object obj) {
	  	// instanceof已经处理了obj == null的情况
	  	if (! (Object instanceof Student)) {
	  		
	  		return false;
	  	}
	  	Student stuObj = (Student) obj;
	  	// 地址相等
	  	if (this == stuObj) {
	  		return true;
	  	}
	  	// 如果对象的姓名,年龄,性别相等.则两个对象相等
	  	if (stuObj.name.equals(this.name) && stuObj.sex.equals(this.sex) && stuObj.age.equals(this.age)) {
	  		return true;
	  	} else {
	  		return false;
	  	}
	 }

	 public String getName() {
	  	return name;
	 }
	 public void setName(String name) {
	  	this.name = name;
	 }
	 public String getSex() {
	  	return sex;
	 }
	 public void setSex(String sex) {
	  	this.sex = sex;
	 }
	 public String getAge() {
	  	return age;
	 }
	 public void setAge(String age) {
	  	this.age = age;
	 }
	 public String getWeight() {
	  	return weight;
	 }
	 public void setName(String weight) {
	  	this.weight = weight;
	 }
	 public String getAddr() {
	  	return addr;
	 }
	 public void setAddr(String addr) {
	  	this.addr = addr;
	 }
}
public static void main(String[] args) {
	Student s1 = new Student();
	s1.setAddr("earth");
	s1.setAge("20");
	s1.setName("Tom");
	s1.setSex("Male");
	s1.setWeight(60f);

	Student s2 = new Student();
	s2.setAddr("Mars");
	s2.setAge("20");
	s2.setName("Tom");
	s2.setSex("Male");
	s2.setWeight(70f);

	if (s1.equals(s2)) {
		System.out.println("s1 == s2");
	} else {
		System.out.println("s1 != s2");
	}
	
	Set set = new HashSet();
	set.add(s1);
	set.add(s2);
	System.out.println(Set);
}
[com.oxford.Student@7852e922, com.oxford.Student@4e25154f]
/*
 * 重写hashCode()方法
 */
@Override
public int hashCode() {
	int result = name.hashCode();
	result = 17 * result + sex.hashCode();
	result = 17 * result + age.hashCode();
	return result;
}

原理分析

HashMap

HashMap组成结构

HashMap的存储

标签:String,避雷,s1,equals,hashCode,对象,hashcode,public
来源: https://www.cnblogs.com/chova/p/15692105.html