equals

Equals vs GetHashCode when comparing objects

。_饼干妹妹 提交于 2019-11-27 22:22:11
Should we override both Equals and GetHashCode properties when implementing a custom class instances comparison? In the following code I have a collection of classes. The class A is compared by the ID , the class B - by Code . using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List<I> bars = new List<I>(); bars.Add(new A() { Id = 1, Code = "one A" }); bars.Add(new B() { Id = 1, Code = "one B" }); bars.Add(new A() { Id = 1, Code = "one A+" }); bars.Add(new B() { Id = 1,

Should one override equals method for asserting the object equality in a unit test?

放肆的年华 提交于 2019-11-27 22:07:41
Let's say we are testing the result of a method by asserting the equality of all the properties of the result object with properties of an expected result object. Should we implement equals method and use Assert.AreEqual(expectedResult, actualResult)... But equals may mean something different in production code. Which is the best practice? Asserting the equality of the objects through overriden equals method or Asserting the equality of all the properties I for one use custom assertions. There are two main reasons: don't force test concerns into production. This means that the meaning of

When do I need to override equals and hashcode methods? [duplicate]

孤人 提交于 2019-11-27 21:26:36
Possible Duplicate: Overriding equals and hashCode in Java If I have class A { int x = 1; } ... A a1 = new A(); A a2 = new A(); a1.equals(a2); If I compare 2 instances of A without override the equals method, will I get expected result? If I compare 2 instances of A without override the equals method, will I get expected result? That depends on what you expect :) The default implementation will give you reference equality - in other words, when you compare two references, equals will only return true if they're references to the same object. You would normally override equals to implement

Java: Use hashCode() inside of equals() for convenience?

烈酒焚心 提交于 2019-11-27 21:18:17
Consider the following test case, is it a bad practice to use the hashCode() method inside of equals as a convenient shortcut? public class Test { public static void main(String[] args){ Test t1 = new Test(1, 2.0, 3, new Integer(4)); Test t2 = new Test(1, 2.0, 3, new Integer(4)); System.out.println(t1.hashCode() + "\r\n"+t2.hashCode()); System.out.println("t1.equals(t2) ? "+ t1.equals(t2)); } private int myInt; private double myDouble; private long myLong; private Integer myIntObj; public Test(int i, double d, long l, Integer intObj ){ this.myInt = i; this.myDouble = d; this.myLong = l; this

MySQL - NULL safe NOT equal operator

ⅰ亾dé卋堺 提交于 2019-11-27 21:11:26
问题 I am just curious. I know about NULL safe equal operator <=>, but is there some NULL safe NOT equal operator, or I have to always use something like that: (tab.id != 1 OR tab.id IS NULL) or someone prefers !(tab.id <=> 1) 回答1: COALESCE(tab.id, 0) != 1 Can be used here if you like it. I goes through the parameters, and returns the first value that isn't NULL . In this case if it's NULL , it will compare 0 != 1 . Although it may use more signs, it's still easier to manage instead of being

Comparing two strings with “==”: when will it work?

点点圈 提交于 2019-11-27 21:02:01
问题 Say you have three strings, String s1 = "string one"; String s2 = new String("string one"); String s3 = "string one"; I know it is true that s1 == s2 is false , but I read somewhere that s1 == s3 is true . Is this correct? Why or why not? 回答1: String literals are interned automatically. Hence s1 == s3 is true. Strings can either be created in the string constant pool or they can be created in the heap space. If you intern a string created in the heap, the string will be in the string constant

two unequal objects with same hashcode

扶醉桌前 提交于 2019-11-27 19:50:54
Hashcode() and equals() concept is 1) If two Objects are equal according to equal(), then calling the hashcode method on each of those two objects should produce same hashcode. and other one is 2) It is not required that if two objects are unequal according to the equal(), then calling the hashcode method on each of the two objects must produce distinct values. I tried and understood first one and this is the code for first point. public class Test { public static void main(String[] args) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(1, 11); map.put(4, 11); System.out

Force a class to override the .equals method

两盒软妹~` 提交于 2019-11-27 18:41:30
I have a bunch of class who implement a common interface : Command. And this bunch of class goes to a Map. To get the Map working correctly, I need to each class who implements Command to override the Object.equals(Object other) method. it's fine. But i whould like to force the overriding of equals. => Have a compilation error when something who implement command dont override equals. It's that possible ? Edit : BTW , i will also need to forcing the override of hashcode... No, you can't. What you can do, however, is use an abstract base class instead of an interface, and make equals() abstract

How default .equals and .hashCode will work for my classes?

此生再无相见时 提交于 2019-11-27 17:28:41
Say I have my own class public class MyObj { /* ... */ } It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode. Once we call equals and hashCode, what are the default implementations? From Object class? And what are they? How the default equals will work? How the default hashCode will work and what will return? == will just check if they reference to the same object, so it's easy, but what about equals() and hashCode() methods? Etienne de Martel Yes, the default implementation is Object's (generally speaking; if you inherit from a class that redefined

What is the difference between IEqualityComparer<T> and IEquatable<T>?

十年热恋 提交于 2019-11-27 17:13:43
I want to understand the scenarios where IEqualityComparer<T> and IEquatable<T> should be used. The MSDN documentation for both looks very similar. IEqualityComparer<T> is an interface for an object that performs the comparison on two objects of the type T . IEquatable<T> is for an object of type T so that it can compare itself to another. When deciding whether to use IEquatable<T> or IEqualityComparer<T> , one could ask: Is there a preferred way of testing two instances of T for equality, or are there several equally valid ways? If there is only one way of testing two instances of T for