如何根据属性比较来自相同 class 的对象?
How to compare objects from a same class based on their attributes?
我有这个对象:
COSTOS Costos = new COSTOS(1781, 359.13, "BISAG.SUP.PUER.TRA.I", "67550T9AT00ZZ");
COSTOS Herramienta = new COSTOS(1795, 299.11, "BISAG.INF.PUER.TRA.I", "67960T2MT02ZZ");
这是我的 class:
public class COSTOS implements Comparable<COSTOS>{
public int referencia;
public double monto;
public String descripcion;
public String NumeroParte;
//Constructor
//getters setters
另外,我实现了 HashCode 和 equals:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((NumeroParte == null) ? 0 : NumeroParte.hashCode());
result = prime * result + ((descripcion == null) ? 0 : descripcion.hashCode());
long temp;
temp = Double.doubleToLongBits(monto);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + referencia;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
COSTOS other = (COSTOS) obj;
if (NumeroParte == null) {
if (other.NumeroParte != null)
return false;
} else if (!NumeroParte.equals(other.NumeroParte))
return false;
if (descripcion == null) {
if (other.descripcion != null)
return false;
} else if (!descripcion.equals(other.descripcion))
return false;
if (Double.doubleToLongBits(monto) != Double.doubleToLongBits(other.monto))
return false;
if (referencia != other.referencia)
return false;
return true;
}
How could i implement a method that could print all the attributes
that are not equals?
我尝试使用 "import java.util.Objects;" 来使用:"Objects.hash(referencia, monto, descripcion, NumeroParte);",这样可能会给我打印结果
如果我理解你的需求是正确的,你想打印出两个对象中不相同的属性值,那么你可以创建一个方法如下。
public void compareAttributes(COSTOS other) {
if (this.getMonto() != other.getMonto()) {
System.out.println("Not equal. This obj : " + this.getMonto()
+ " Other obj : " + other.getMonto());
}
// you can do the same for the remaining attributes.
}
编辑:
正如@Andreas 在评论中指出的那样,您应该将此方法放在 COSTOS
class 本身中,以便可以轻松比较每个对象。
首先,可以使用 Java 7:
中添加的空安全 Objects
辅助方法来简化您的方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Objects.hashCode(this.NumeroParte);
result = prime * result + Objects.hashCode(this.descripcion);
result = prime * result + Double.hashCode(this.monto);
result = prime * result + this.referencia;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
COSTOS other = (COSTOS) obj;
return (Objects.equals(this.NumeroParte, other.NumeroParte)
&& Objects.equals(this.descripcion, other.descripcion)
&& Double.doubleToLongBits(this.monto) == Double.doubleToLongBits(other.monto)
&& this.referencia == other.referencia);
}
How could i implement a method that could print all the attributes that are not equals?
要打印差异,请执行与 equals
方法相同的比较:
public void printDifferences(COSTOS other) {
if (! Objects.equals(this.NumeroParte, other.NumeroParte))
System.out.println("Different NumeroParte: " + this.NumeroParte + " != " + other.NumeroParte);
if (! Objects.equals(this.descripcion, other.descripcion))
System.out.println("Different descripcion: " + this.descripcion + " != " + other.descripcion);
if (Double.doubleToLongBits(this.monto) != Double.doubleToLongBits(other.monto))
System.out.println("Different monto: " + this.monto + " != " + other.monto);
if (this.referencia != other.referencia)
System.out.println("Different referencia: " + this.referencia + " != " + other.referencia);
}
我有这个对象:
COSTOS Costos = new COSTOS(1781, 359.13, "BISAG.SUP.PUER.TRA.I", "67550T9AT00ZZ");
COSTOS Herramienta = new COSTOS(1795, 299.11, "BISAG.INF.PUER.TRA.I", "67960T2MT02ZZ");
这是我的 class:
public class COSTOS implements Comparable<COSTOS>{
public int referencia;
public double monto;
public String descripcion;
public String NumeroParte;
//Constructor
//getters setters
另外,我实现了 HashCode 和 equals:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((NumeroParte == null) ? 0 : NumeroParte.hashCode());
result = prime * result + ((descripcion == null) ? 0 : descripcion.hashCode());
long temp;
temp = Double.doubleToLongBits(monto);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + referencia;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
COSTOS other = (COSTOS) obj;
if (NumeroParte == null) {
if (other.NumeroParte != null)
return false;
} else if (!NumeroParte.equals(other.NumeroParte))
return false;
if (descripcion == null) {
if (other.descripcion != null)
return false;
} else if (!descripcion.equals(other.descripcion))
return false;
if (Double.doubleToLongBits(monto) != Double.doubleToLongBits(other.monto))
return false;
if (referencia != other.referencia)
return false;
return true;
}
How could i implement a method that could print all the attributes that are not equals?
我尝试使用 "import java.util.Objects;" 来使用:"Objects.hash(referencia, monto, descripcion, NumeroParte);",这样可能会给我打印结果
如果我理解你的需求是正确的,你想打印出两个对象中不相同的属性值,那么你可以创建一个方法如下。
public void compareAttributes(COSTOS other) {
if (this.getMonto() != other.getMonto()) {
System.out.println("Not equal. This obj : " + this.getMonto()
+ " Other obj : " + other.getMonto());
}
// you can do the same for the remaining attributes.
}
编辑:
正如@Andreas 在评论中指出的那样,您应该将此方法放在 COSTOS
class 本身中,以便可以轻松比较每个对象。
首先,可以使用 Java 7:
中添加的空安全Objects
辅助方法来简化您的方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Objects.hashCode(this.NumeroParte);
result = prime * result + Objects.hashCode(this.descripcion);
result = prime * result + Double.hashCode(this.monto);
result = prime * result + this.referencia;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
COSTOS other = (COSTOS) obj;
return (Objects.equals(this.NumeroParte, other.NumeroParte)
&& Objects.equals(this.descripcion, other.descripcion)
&& Double.doubleToLongBits(this.monto) == Double.doubleToLongBits(other.monto)
&& this.referencia == other.referencia);
}
How could i implement a method that could print all the attributes that are not equals?
要打印差异,请执行与 equals
方法相同的比较:
public void printDifferences(COSTOS other) {
if (! Objects.equals(this.NumeroParte, other.NumeroParte))
System.out.println("Different NumeroParte: " + this.NumeroParte + " != " + other.NumeroParte);
if (! Objects.equals(this.descripcion, other.descripcion))
System.out.println("Different descripcion: " + this.descripcion + " != " + other.descripcion);
if (Double.doubleToLongBits(this.monto) != Double.doubleToLongBits(other.monto))
System.out.println("Different monto: " + this.monto + " != " + other.monto);
if (this.referencia != other.referencia)
System.out.println("Different referencia: " + this.referencia + " != " + other.referencia);
}