简体   繁体   English

比较两个对象是否相等:

[英]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.

相关问题 使用 .equals() 和 == 运算符比较两个对象 - Compare two objects with .equals() and == operator 实现一个 equals() 方法来比较 java 中用整数填充的两个对象中的内容 - implementing an equals() method to compare contents in two objects filled with integers in java 如何比较两组对象不使用 Comparator 和 Streams 覆盖 equals/hashCode - How to compare two Sets of objects Not overriding equals/hashCode with Comparator and Streams 比较两棵树的相等方法 - Equals method to compare two trees 如何按可能具有浮点值但没有正确等号的成员比较两个对象? - How to compare two objects by members that might have floating point values and do not have a proper equals? 当您使用equals()比较两个对象时,equals()到底有什么比较? - what does the equals() exactly comparing when you using it to compare two objects? 尝试比较来自同一类的两个对象时出现 AssertionFailedError 错误(在覆盖 Equals() 和 Hashcode() 之后) - AssertionFailedError error when trying to compare two objects from the same class(after Overriding Equals() and Hashcode()) Equals使用String对象进行比较的assertEquals方法 - Equals Method for assertEquals with String-objects to compare 编写equals方法比较两个数组 - Writing an equals method to compare two arrays 尝试使用 equals() 比较 Java 中的两个字符串 - Trying to compare two strings in Java using equals()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM