简体   繁体   English

两个相等的对象必须具有相同的toString输出吗?

[英]Do two equal objects have to have the same toString output?

Do two equal objects have to have the same toString output? 两个相等的对象必须具有相同的toString输出吗?

In code, does the following have to hold in general? 在代码中,以下是否必须保持一般?

if(o1.equals(o2))
  return o1.toString().equals(o2.toString()) // always true?

I am asking because I have just written a toString method for which the above statement does not hold. 我问,因为我刚刚编写了一个toString方法,上面的语句不能保存。 I could not find any hint at the documentation, but I want to get sure my toString method does not break any contract rules. 我在文档中找不到任何提示,但我想确保我的toString方法不会违反任何合同规则。

No, they do not have to have the same toString() output in order to be equal. 不,它们不必具有相同的toString()输出以便相等。 There is no contract in Java that states the equals() method must be true for both the object itself and its toString() method. Java中没有任何契约声明equals()方法对于对象本身及其toString()方法都必须为true。

The only contract equals() has with other methods is with hashCode() : 与其他方法唯一的合约equals()hashCode()

Note that it is generally necessary to override the hashCode method whenever this method [equals] is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. 注意,每当重写此方法[equals]时,通常需要覆盖hashCode方法,以便维护hashCode方法的常规协定,该方法声明相等的对象必须具有相等的哈希码。

The contract it has irrespective of other methods is: 与其他方法无关的合同是:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true. 它是自反的:对于任何非空引用值x,x.equals(x)应该返回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. 它是对称的:对于任何非空引用值x和y,当且仅当y.equals(x)返回true时,x.equals(y)才应返回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. 它是传递性的:对于任何非空引用值x,y和z,如果x.equals(y)返回true而y.equals(z)返回true,则x.equals(z)应返回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. 它是一致的:对于任何非空引用值x和y,x.equals(y)的多次调用始终返回true或始终返回false,前提是不修改在对象的equals比较中使用的信息。

  • For any non-null reference value x, x.equals(null) should return false. 对于任何非空引用值x,x.equals(null)应返回false。

Source: https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object- 资料来源: https//docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-

Edit: Some people are saying Effective Java by Joshua Bloch says the toString() method should use the same fields as the equals() method. 编辑:有人说Joshua Bloch的Effective Java说toString()方法应该使用与equals()方法相同的字段。 This is not true. 这不是真的。 He states: 他说:

While it isn't as important as obeying the equals and hashCode contracts (Item 8, Item 9), providing a good toString implementation makes your class much more pleasant to use. 虽然它不像遵守equals和hashCode合同那样重要(Item 8,Item 9),但是提供一个好的toString实现会让你的类更加愉快。

What he classifies as a "good implementation" is: 他将其归类为“良好实施”的是:

When practical, the toString method should return all of the interesting information contained in the object, as in the phone number example just shown. 在可行的情况下,toString方法应返回对象中包含的所有有趣信息,如刚刚显示的电话号码示例所示。 It is impractical if the object is large or if it contains state that is not conducive to string representation. 如果对象很大或者它包含不利于字符串表示的状态,那是不切实际的。 Under these circumstances, toString should return a summary such as “Manhattan white pages (1487536 listings)” or “Thread[main,5,main]”. 在这些情况下,toString应该返回一个摘要,例如“Manhattan white pages(1487536 listings)”或“Thread [main,5,main]”。

So no, it in no way relates to the equals() method. 所以不,它与equals()方法无关。

There is no requirement for this. 没有要求。 There is a dependency between equals and hashCode : equal objects must return the same hashCode value. equalshashCode之间存在依赖关系:相等的对象必须返回相同的hashCode值。 toString is only used for printing the object. toString仅用于打印对象。

There is no contract for toString, only for equals and hashCode. toString没有合约,仅适用于equals和hashCode。 And from my point of view it also doesn't make sense. 从我的观点来看,它也没有意义。

Imagine o1 and o2 are of class Person{}, then you get only the string of the reference. 想象一下o1和o2属于Person {}类,那么你只得到引用的字符串。

There's no "requirement" that hashCode match equals , only a best practice that it do so, or your program won't make sense. 有没有“规定”指出hashCode匹配equals ,只有一个最佳实践,它这样做,或者你的程序将没有意义。 Same with toString . toString相同。 It should be consistent with equals , as should compareTo if used. 它应符合equals ,因为应compareTo如果使用。 See Effective Java by Joshua Bloch for the rationale. 请参阅Joshua Bloch撰写的Effective Java的基本原理。 People who tell you otherwise need to read that book because they're mistaken. 告诉你的人否则需要阅读那本书,因为他们错了。

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

相关问题 同一个类的两个类是否具有相同的哈希码,它们被认为是相同的吗? - Do two classes of the same class have the same hashcode and are they considered equal? 具有相同哈希值的对象应该相等吗? - Should to objects which have the same hash be equal? 数组toString的对象也具有toString方法 - Arrays toString with objects that also have a toString method 如何使用等于哈希码和 toString 的两个具有循环的对象来龙目岛? - How to Lombok with equals hashcode and toString with two objects that have a cycle? 两个对象有相同的引用肯定是一样的吗? - two objects have same reference are definitely same? 检查两个对象是否具有相同的属性 - Check if two objects have the same attributes 相等的对象必须具有相等的哈希码? - Equal Objects must have equal hashcodes? 当你有两个构造函数时覆盖toString() - Overriding toString() when you have two constructors 如何实例化同一对象的两个线程,并使对象打印不同的东西 - How do I instantiate two threads of the same object, and have the objects print different things 如何在同一输入上运行两个不同的映射器,并将其输出发送到单个reducer? - How do I run two different mappers on the same input and have their output sent to a single reducer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM