java两个对象值的比较_Java:比较两个对象中所有属性值是否相等

例以下述Java类:java

import java.io.Serializable;

import java.util.List;

public class Bean_Topology implements Serializable {

private static final long serialVersionUID = 1L;

public static long getSerialversionuid() {

return serialVersionUID;

}

private Long topology_pk;

private String topology_id;

public String getTopology_id() {

return topology_id;

}

public void setTopology_id(String topology_id) {

this.topology_id = topology_id;

}

public Long getTopology_pk() {

return topology_pk;

}

public void setTopology_pk(Long topology_pk) {

this.topology_pk = topology_pk;

}

@Override

public String toString() {

return "当前拓扑的PK为:" + topology_pk + ",ID为:" + topology_id;

}

}

复制代码

以下想判断下面两个对象中所有属性值是否一致时,有哪些办法呢? sql

Bean_Topology topology1 = new Bean_Topology();

topology1.setTopology_id("1");

Bean_Topology topology2 = new Bean_Topology();

topology2.setTopology_pk(1L);

topology2.setTopology_id("1");

复制代码

方法一:重写Bean_Topology的equals方法和hashcode方法,代码以下

你可能感兴趣的:(java两个对象值的比较)