equals

Why use GetHashCode() over Equals()?

a 夏天 提交于 2019-11-27 07:40:03
问题 HashSet<T>.Add first compares the results of GetHashCode . If those are equal, it calls Equals . Now, my understanding is in order to implement GetHashCode , something must be done with the fields of an object. A simple example implementation can be found at What is the best algorithm for an overridden System.Object.GetHashCode?. In my test comparing both on 1.000.000 pairs of objects filled with random data, performance is more or less equal between the two. GetHashCode is implemented as in

Why does 1234 == '1234 test' evaluate to true? [duplicate]

只愿长相守 提交于 2019-11-27 07:02:13
Possible Duplicate: php == vs === operator An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true? (1234 == '1234 test') Lucas Green Because you are using the == (similarity) operator and PHP is coercing the string to an int. To resolve it use the === (equality) operator, which checks not only if the value is the same, but also if the data type is the same, so "123" string and 123 int won't be considered equal. In PHP (and JavaScript -- which has slightly different behavior), the comparison operator == works differently than it does in strongly-typed

Simplify Overriding Equals(), GetHashCode() in C# for Better Maintainability

不打扰是莪最后的温柔 提交于 2019-11-27 06:48:19
问题 I find my self overriding Equals() and GetHashCode() frequently to implement the semantic that business objects with identical property values are equal. That leads to code that is repetitive to write and fragile to maintain (property gets added and one/both of the overrides are not updated). The code ends up looking something like this (comments on the implementation are welcome): public override bool Equals(object obj) { if (object.ReferenceEquals(this, obj)) return true; MyDerived other =

When “” == s is false but “”.equals( s ) is true

给你一囗甜甜゛ 提交于 2019-11-27 06:16:36
EDIT Thanks for the prompt responses. Please see what the real question is. I have made it bold this time. I do understand the difference between == and .equals. So, that's not my question (I actually added some context for that) I'm performing the validation below for empty strings: if( "" == value ) { // is empty string } In the past when fetching values from the db or deserializing objects from another node, this test failed , because the two string instances were indeed different object references, albeit they contained the same data. So the fix for those situations was if( "".equals(

Why == operator and equals() behave differently for values of AnyVal in Scala

我的未来我决定 提交于 2019-11-27 05:45:16
问题 In the scaladoc of scala.Any , the operator == (or, method == ) is explained: The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that) http://www.scala-lang.org/api/current/#scala.Any For objects of subclasses of AnyRef , I can understand it easily, and I didn't see any strange things. However, for values of AnyVal , (I mean Int , Double , Long , and so on,) the above definition is somewhat tricky ( 1 eq null ? This does not compile if we do not convert 1 to

What's the best way to compare Double and Int?

二次信任 提交于 2019-11-27 05:14:37
The following code in C# doesn't work: int iValue = 0; double dValue = 0.0; bool isEqual = iValue.Equals(dValue); So, the question: what's the best way to compare Double and Int? LBushkin You really can't compare floating point and integral values in a naive way; particularly, since there's the classic floating point representation challenges . What you can do is subtract one from the other and see if the difference between them is less than some precision you care about, like so: int iValue = 0; double dValue = 0.0; var diff = Math.Abs(dvalue - iValue); if( diff < 0.0000001 ) // need some min

Overriding equals() & hashCode() in sub classes … considering super fields

泪湿孤枕 提交于 2019-11-27 05:08:59
问题 Is there a specific rule on how Overriding equals() & hashCode() in sub classes considering super fields ?? knowing that there is many parameters : super fields are private/public , with/without getter ... For instance, Netbeans generated equals() & hashCode() will not consider the super fields ... and new HomoSapiens("M", "80", "1.80", "Cammeron", "VeryHot").equals( new HomoSapiens("F", "50", "1.50", "Cammeron", "VeryHot")) will return true :( public class Hominidae { public String gender;

Using '==' instead of .equals for Java strings [duplicate]

ぃ、小莉子 提交于 2019-11-27 05:07:58
Possible Duplicate: What makes reference comparison (==) work for some strings in Java? I know this has been asked before , but in spite of recommendations to use .equals() instead of the == comparison operator, I found that == works all the time: String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1 == s2); // true Can anyone give me an example of the == operator failing? This is because you're lucky. The == operator in Java checks for reference equality : it returns true if the pointers are the same. It does not check for contents equality. Identical strings found at compile-time

When Should a .NET Class Override Equals()? When Should it Not?

独自空忆成欢 提交于 2019-11-27 05:04:57
The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states in part Overriding operator == in non-immutable types is not recommended. The newer .NET Framework 4 documentation Guidelines for Implementing Equals and the Equality Operator (==) omits that statement, although one post in Community Content repeats the assertion and references the older documentation. It seems that it is reasonable to override Equals() at least for some trivial mutable classes, such as public class ImaginaryNumber { public double RealPart { get; set; } public double

equals and hashCode of these entities (Spring MVC + Hibernate)

耗尽温柔 提交于 2019-11-27 04:53:21
问题 Someone can please suggest me how I can do equals and hashCode methods of these entities? This is a many-to-many relationship between a Gara (Contest) and Agenzia (Agency): One contest has many Agency, one Agency can be in more Contest. I tried some implementations but or I get Stackoverflow error, or, when I'm updating a Gara (Contest), I can't update the set of Agenzie (Agencies) because i get this error: org.springframework.dao.DuplicateKeyException: a different object with the same