equals

Linq 'equals' keyword Revisited - Does it compare values and references to objects?

痞子三分冷 提交于 2019-12-24 01:53:29
问题 This question is a follow on to link text hence the revisited in the title. I raise this as a new question as the accepted answer and comment made under the original question both suggest that the equals keyword used in the join query refers to only value types within the comparison. I believe this is misleading as follows. Behind the scenes the default equality comparer is used to compare keys using a keyed lookup. The join is implemented in Enumerable.Join and the key can be either a value

Union-ing two custom classes returns duplicates

笑着哭i 提交于 2019-12-23 19:54:25
问题 I have two custom classes, ChangeRequest and ChangeRequests , where a ChangeRequests can contain many ChangeRequest instances. public class ChangeRequests : IXmlSerializable, ICloneable, IEnumerable<ChangeRequest>, IEquatable<ChangeRequests> { ... } public class ChangeRequest : ICloneable, IXmlSerializable, IEquatable<ChangeRequest> { ... } I am trying to do a union of two ChangeRequests instances. However, duplicates do not seem to be removed. My MSTest unit test is as follows: var cr1 = new

Java / JUnit - comparing two polynomial objects

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:21:03
问题 I have a Java class called Term holding polynomials like below public Term(int c, int e) throws NegativeExponent { if (e < 0) throw new NegativeExponent(); coef = c; expo = (coef == 0) ? 1 : e; } I also have an equals method in the same class like below @Override public boolean equals(Object obj) { } I am stuck with how to code how to compare these 2 Term objects Within my JUnit test file I am using the test below to try and test the equals method import static org.junit.Assert.*; import org

An efficient equals(Object o) implementation

左心房为你撑大大i 提交于 2019-12-23 19:16:05
问题 I read this SO post after I wrote out the title but still decided to go through with the question on bug-proof implementations of equals in Java. This is my normal implementation @Override public boolean equals(Object o){ if(o == null) return false; if(o instanceof CompositePk == false) return false; if(this == o) return true; CompositePk that = (CompositePk)o; return new EqualsBuilder().append(this.id, that.id) .append(this.bucketId, that.bucketId) .isEquals(); } using Apache's EqualsBuilder

Apart from immutable value objects, when should I override `equals()`?

淺唱寂寞╮ 提交于 2019-12-23 17:27:35
问题 It's clear that equals() (and of course hashCode() ) are valuable when you're dealing with immutable value objects -- map keys, strongly typed field values that you need to compare across the objects that contain them, etc. But apart from value objects, how often is it really likely that you'll have two independently constructed instances and want them to be equal ? It's hard for me to imagine a realistic scenario where referential equality wouldn't, de facto , get you what you want; and in

Implementing IEquatable<T> in a mutable type

被刻印的时光 ゝ 提交于 2019-12-23 16:10:44
问题 I have a class that represents an external physical measuring device. The simplified version looks like this: public class Device { public string Tag { get; set; } public int Address { get; set; } } Tag is a user-defined value for identifying the device. Address is the value used by an adapter to communicate with the device. If two instances of Device have the same Address , then the same external measuring device will be used. I'd like to mimic that behavior in code (for using methods like

C#: Same Object have to Same HashCode?

跟風遠走 提交于 2019-12-23 14:46:27
问题 Let's assume I have two objects called K and M if(K.Equals(M)) { } If that's true, K and M always has the same HashCode ? Or It depends on the programming language ? 回答1: The contract for GetHashCode() requires it, but since anyone can make their own implementation it is never guaranteed. Many classes (especially hashtables) require it in order to behave correctly. If you are implementing a class, you should always make sure that two equal objects have the same hashcode. If you are

Is there a number “value equals”?

混江龙づ霸主 提交于 2019-12-23 10:56:58
问题 By default, Java does Binary Numeric Promotion for primitives, but does not do the same thing for objects. Here's a quick test to demonstrate: public static void main(String... args) { if(100 == 100L) System.out.println("first trial happened"); if(Integer.valueOf(100).equals(Long.valueOf(100))) { System.out.println("second trial was true"); } else { System.out.println("second trial was false"); } if(100D == 100L) System.out.println("third trial, fun with doubles"); } Output: first trial

Object.Equals is virtual, but Object.operator== does not use it in C#?

99封情书 提交于 2019-12-23 10:20:08
问题 I got hit by a strange "asymmetry" in C# that I do not really understand. See the following code: using System; using System.Diagnostics; namespace EqualsExperiment { class Program { static void Main(string[] args) { object apple = "apple"; object orange = string.Format("{0}{1}", "ap", "ple"); Console.WriteLine("1"); Debug.Assert(apple.Equals(orange)); Console.WriteLine("2"); Debug.Assert(apple == orange); Console.WriteLine("3"); } } } It might be obvious for all you .NET gurus, but the 2nd

Why is there a need to override hashcode if I override the 'equals' method in Java?

谁都会走 提交于 2019-12-23 09:34:46
问题 I know there is a need to override hashcode whenever the equals method is overridden in Java. That is merely a contract. I am trying to understand the logic behind this. I was reading *Effective Java by Joshua Bloch, and I came across this code (Item 9, page 45): import java.util.HashMap; import java.util.Map; public final class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; public PhoneNumber(int areaCode, int prefix, int lineNumber) {