Rewrite hashCode and equals method of the Object class first attempt

## Why rewrite?

Experience in project development needs to Object object as HashMap of key scene used, in order to ensure the uniqueness of the HashMap key, it is the object hashCode method has been rewritten and rewriting the equals method.

Code Example ##

 

package com.xxx.xxx;

/**
* @Author: 
* @Date: 
* @Description:
*/
public class ColumnDTO {

private String columnid;

private String columnname;

private String nodepath;

private String upperid;

private String linkurl;


public String getColumnid() {
return columnid;
}

public void setColumnid(String columnid) {
this.columnid = columnid;
}

public String getColumnname() {
return columnname;
}

public void setColumnname(String columnname) {
this.columnname = columnname;
}

public String getNodepath() {
return nodepath;
}

public void setNodepath(String nodepath) {
this.nodepath = nodepath;
}

public String getUpperid () {
 return upperid;
}

public void setUpperid(String upperid) {
this.upperid = upperid;
}

public String getLinkurl() {
return linkurl;
}

public void setLinkurl(String linkurl) {
this.linkurl = linkurl;
}


/**
* Override the hashCode method and the equals method to ensure that the object can be used as HashMap using the Key
*/

@Override
public int hashCode() {
int result = 17;
result = 31 * result + (columnid == null ? 0 : columnid.hashCode());
result = 31 * result + (columnname == null ? 0 : columnname.hashCode());
result = 31 * result + (nodepath == null ? 0 : nodepath.hashCode());
result = 31 * result + (upperid == null ? 0 : upperid.hashCode());
result = 31 * result + (linkurl == null ? 0 : linkurl.hashCode());
return result;
}

@Override
public  Boolean the equals (Object obj) {
 IF ( the this == obj) {
 return  to true ; // equal address 
}

IF (obj == null ) {
 return  to false ; // Nonemptiness: For any non-null reference x, x.equals (null) should return false. 
}

if(obj instanceof ColumnDTO){
ColumnDTO other = (ColumnDTO) obj;
//需要比较的字段相等,则这两个对象相等
if(this.columnid.equals(other.columnid)
&& this.columnname.equals(other.columnname)
&& this.nodepath.equals(other.nodepath)
&& this.linkurl.equals(other.linkurl)
&& this.upperid.equals(other.upperid)){
return true;
}
}
return false;
}
}

 

 

## focus and summary

 

@Override
public int hashCode() {
int result = 17;
result = 31 * result + (columnid == null ? 0 : columnid.hashCode());
result = 31 * result + (columnname == null ? 0 : columnname.hashCode());
result = 31 * result + (nodepath == null ? 0 : nodepath.hashCode());
result = 31 * result + (upperid == null ? 0 : upperid.hashCode());
result = 31 * result + (linkurl == null ? 0 : linkurl.hashCode());
return result;
}

 


> HashCode similar process is required for each member of a variable, the final result will be returned as hashCode value.

 

@Override
public boolean equals(Object obj) {
if(this == obj){
return true;//地址相等
}

IF (obj == null ) {
 return  to false ; // Nonemptiness: For any non-null reference x, x.equals (null) should return false. 
}

if(obj instanceof ColumnDTO){
ColumnDTO other = (ColumnDTO) obj;
//需要比较的字段相等,则这两个对象相等
if(this.columnid.equals(other.columnid)
&& this.columnname.equals(other.columnname)
&& this.nodepath.equals(other.nodepath)
&& this.linkurl.equals(other.linkurl)
&& this.upperid.equals(other.upperid)){
return true;
}
}
return false;
}

 

 

> Need to be a member variable for each comparison, all member variables are the same only returns true, otherwise false.

Guess you like

Origin www.cnblogs.com/yuhuashang-edward/p/11015031.html