把对象放进map中必须要重写hashcode、equals

、、、

package com.myym;

public class person {

  private String name;

  private Integer age;

  private String address;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((address == null) ? 0 : address.hashCode());

result = prime * result + ((age == null) ? 0 : age.hashCode());

result = prime * result + ((name == null) ? 0 : name.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;

person other = (person) obj;

if (address == null) {

if (other.address != null)

return false;

} else if (!address.equals(other.address))

return false;

if (age == null) {

if (other.age != null)

return false;

} else if (!age.equals(other.age))

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}


}

、、、

你可能感兴趣的:(把对象放进map中必须要重写hashcode、equals)