English 中文(简体)
同等方法的过境性质
原标题:Transitive nature of equals method
  • 时间:2011-10-08 13:45:27
  •  标签:
  • java

<代码>等于(object)方法的合同具体规定了以下4种特性:重修、校对、中转和一致性。 虽然我理解不服从依赖性、不对称性和一致性的危险,而且能够肯定地同意其服从于过境的好处,但我还是想知道,如果它侵犯过境财产,会带来什么损害?

具体来说, Java图书馆(或各种第三方图书馆)中哪些需要依赖<条码>平等才能进行正确工作? 我的理解是,如果其他3种财产得到妥善执行,则收集框架将发挥作用。

最佳回答

a、b、c

a == a, b == b, c == c (reflexive)
a == b, b == a
b == c, c == b
a != c, c != a

(Pseudocode,x = y <>code>x.qualitys(y)

现在请在一组物体中添加:

Set s = new HashSet(); // Set implementation doesn t matter
s.add(b); // s = [b]
s.add(a); // s doesn t change, because a == b
s.add(c); // s doesn t change, because c == b

相比之下,如果要以不同顺序增加:

Set s = new HashSet();
s.add(a); // s = [a]
s.add(b); // s doesn t change, because b == a
s.add(c); // s = [a,c], because c != a

That is clearly counter-intuitive and doesn t match the behavior one would expect of a set. For example, it would mean that the union of two sets (i.e. the state of s after s.addAll(someOtherSet)) could depend on the implementation (order of elements) of someOtherSet.

问题回答

考虑目标a=b=c, != c(非过境平等)

第一个问题是 has码(hcode)合同,要求 has形码在物体平等时平等。 并且,你将能够在同一组别中增加一个和c项——这可能导致在意外地点出现微妙的问题。

Integer a = new Integer(1);
Integer b = new Integer(1);

a = 1 是真实的,b = 1 是真实的,但a = b不是真实的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签