[英]compare two objects whether they are equals:
I have a list with two Box
objects which are not the same (They have differ in id, address, quantity) and I need to get the index of the last item in the list which is 1
but I am always getting that the both objects are equal and the index of the last object is 0
and not 1
: 我有一个包含两个不相同的
Box
对象的列表(它们在id,地址,数量上有所不同),我需要获取列表中最后一项的索引为1
但我总是得到这两个对象相等,并且最后一个对象的索引为0
而不是1
:
Why are the both objects the same and how to get the index of the last box in the list? 为什么两个对象都相同,以及如何获取列表中最后一个框的索引? Am I missing something?
我想念什么吗?
System.out.println("##--## boxList size: " + boxList.size());
if( boxList.get(0).equals(boxList.get(1))){
System.out.println("##--## the both boxes are equals");
}
for(Box boxB: boxList){
System.out.println("##--## boxB id: " + boxB.id + " box address: " + boxB.store.address + " ,box quantity: " + boxB.quantity + " x: " + boxList.indexOf(boxB));
}
Box lastFirstLoop = boxList.get(1);
int indexTemp = boxList.indexOf(lastFirstLoop);
System.out.println("##--## indexTemp: " + indexTemp );
The output is: 输出为:
##--## boxList size: 2
##-## box列表大小:2
##--## the both boxes are equals##-##两个方框都相等
##--## boxB id: 1513682911061 box address: DS/1-1-1-1/A ,box quantity: 120 x: 0##-## boxB ID:1513682911061盒子地址:DS / 1-1-1-1 / A,盒子数量:120 x:0
##--## boxB id: 1513682911062 box address: DS/1-1-2-1/A ,box quantity: 18 x: 0##-## boxB ID:1513682911062盒子地址:DS / 1-1-2-1 / A,盒子数量:18 x:0
##--## indexTemp: 0##-## indexTemp:0
Edit: 编辑:
@Override
public boolean equals(final Object other) {
if (!(other instanceof Box)) {
return false;
}
return new EqualsBuilder().appendSuper(super.equals(other)).isEquals();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
if (this.hashCode == 0) {
this.hashCode = new HashCodeBuilder().appendSuper(super.hashCode()).toHashCode();
}
return this.hashCode;
}
Assuming the "Box" is your custom class you should take a look here: https://stackoverflow.com/a/27609/6705466 假设“盒子”是您的自定义类,则应在此处查看: https : //stackoverflow.com/a/27609/6705466
It guides how to override equals(Object o) and hashCode(). 它指导如何重写equals(Object o)和hashCode()。 You always should override those both together, and whenever equals returns true the hashCode should be the same.
您始终应该一起覆盖这两个参数,并且每当equals返回true时,hashCode应该相同。
My other suggestion is, add a little if statement before accessing index in the list (Just to avoid IndexOutOufBoundsException, possibly null check would be a nice to have) 我的另一个建议是,在访问列表中的索引之前添加一些if语句(只是为了避免IndexOutOufBoundsException,可能最好使用null检查)
First of all you need to **Override** your equals() method since **Box** is your Custom class. If you want to use .equals() method properly. Example is shown below:
class Box {
/*your class body*/
// Overriding equals() to compare two Box objects
@Override
public boolean equals(Object o) {
Box box = (Box) o;
if(id.equals(box.id)){
return true;
}
return false;
}
}
And for indexOf() method you need to override hashCode() method.Shown below: 对于indexOf()方法,您需要重写hashCode()方法。如下所示:
@Override
public int hashCode() {
return Objects.hash(id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.