list中的equals()表示两个包含相同元素的列表被定义为相等。 但是仅在对象类的equals()的哈希码相等时才返回true。 这意味着列表中的equals()会覆盖对象类中的equals方法。与来自字符串的equals()相同。只要它们具有相同的字符,它们就返回true。
因此,每当我将类型声明为String或使用列表类(如arraylist)时, 是否会自动覆盖equals()?
答案 0 :(得分:4)
equals()是否会自动重设?
答案:是的,完全正确,如果您要求覆盖的.equals()
方法在运行时自动调用
**对象类是Java中每个类的父类,它由.equals()
方法组成,该方法比较对象引用
但是String
类,Wrapper类(Integer,Long etc..)
和Collections类(ArrayList, hashSet etc..)
被覆盖了.equals()
方法,用于比较对象而不是对象引用中的内容
为避免混淆,这是一个明显的例子
public class Main2 {
public static void main(String[] args) {
List<String> l1 = new ArrayList<>();
l1.add(new String("hello"));
List<String> l2 = new ArrayList<>();
l2.add(new String("hello"));
System.out.println(l1.equals(l2)); //true
List<Test> t1 = new ArrayList<>();
t1.add(new Test());
List<Test> t2 = new ArrayList<>();
t2.add(new Test());
System.out.println(t1.equals(t2)); //false
}
}
class Test{
}
在以上示例中,比较List<String>
将返回true,因为.euqals()
中的String
方法被覆盖以比较内容
但是在比较Lits<Test>
时,即使两个对象都为空,也会返回false,因为.equals()
类中的Test
方法默认情况下不会被覆盖,它将调用Object
类{ {1}}方法与.equals()
一样比较对象的引用
Google Question 对象类equals方法比较哈希码吗?
答案
java.lang.Object类要求在对象上调用hashCode()方法时,使用equals()方法比较相等的任何两个对象必须产生相同的整数结果[API 2014]。 equals()方法用于确定对象实例之间的逻辑等效性.2018年2月12日
答案 1 :(得分:3)
equals()是否会自动覆盖?
否。方法不会不会“自动”覆盖。
您可以看一下代码-两个类都有自己的equals
和hashCode
实现。此实现是在运行时使用的。如果您要编写自己的类,则可能会实现equals
和hashCode
。
但是从对象类equals()仅在其哈希码相等时才返回true。
我认为您(以及其他答案的原始版本)对equals
上的文档有误解:
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:
It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
其中唯一涉及hashCode的部分位于结尾,它指定相等的对象必须具有相同的哈希码-这不是自动的,按照惯例,{{1 }} 将工作。
比较HashMap
的值不是hashCode
的默认实现,并且绝不应该是equals
的实现-可能有多个不相等的对象,它们的结果相同equals
。规则是确保如果对象相等,则hashCode
实现会返回相同的值。
As an example,两者将输出相同的hashCode
,但显然不相等:
hashCode
建议进一步阅读:this related question。
答案 2 :(得分:0)
否,当在列表中比较String时,.equals()
不会被魔术覆盖。
java中的String类已经在其定义中覆盖了.equals()
方法,以默认情况下比较字符。
含义,即使您没有此操作,也不会列出列表:
String a = new String("abc");
String b = new String("abc");
System.out.println(a.equals(b));
然后,您的输出将为true
请参阅:https://docs.oracle.com/javase/7/docs/api/java/lang/String.html