简体   繁体   English

List.equals() 在覆盖 java.lang.Object.equals() 时是否执行了预期的行为?

[英]Does List.equals() performed intended behavior while overriding java.lang.Object.equals()?

Let's say I have this ArrayList in a class:假设我在 class 中有这个 ArrayList:

private ArrayList<Integer> someList = new ArrayList<Integer>();

If I'm overriding the java.lang.equals() method in a class containing the ArrayList, and I want to compare if another Object's ArrayList is equal: If I'm overriding the java.lang.equals() method in a class containing the ArrayList, and I want to compare if another Object's ArrayList is equal:

@Override
public boolean equals(Object o) {
  this.someList.equals(o.someList);
}

Would the ArrayList equals method work in comparing the size and contents of the other ArrayList, or would it just call the current overriden equals method? ArrayList equals 方法是否可以用于比较其他 ArrayList 的大小和内容,或者它只是调用当前覆盖的 equals 方法?

The ArrayList instance calls it's equals method. ArrayList实例调用它的 equals 方法。 The overwritten equals method can be called only from instances of your class.覆盖的 equals 方法只能从 class 的实例中调用。 So the answer is yes, it will work in comparing the size and contents of the other ArrayList .所以答案是肯定的,它将用于比较其他ArrayList的大小和内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM