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
:
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
##--## the both boxes are equals
##--## boxB id: 1513682911061 box address: DS/1-1-1-1/A ,box quantity: 120 x: 0
##--## boxB id: 1513682911062 box address: DS/1-1-2-1/A ,box quantity: 18 x: 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;
}