equals

Equals, GetHashCode, EqualityComparers and fuzzy equality

与世无争的帅哥 提交于 2019-12-02 00:05:33
For an object with properties A, B, C, D, StartDate and EndDate if I wanted to implement something where any two objects are equal if they have identical A, B and C and overlapping date range, how would that be done? I have tried creating an EqualityComparer like so public override bool Equals(RateItem x, RateItem y) { bool equal = true; if ((x.A != y.A || x.B != y.B || x.C != y.C || (x.StartDate < y.StartDate && x.EndDate <= y.StartDate) || (x.StartDate > y.StartDate && y.EndDate <= x.StartDate))) { equal = false; } return equal; } But it seems lots of places in the framework ignore Equals

How to Compare a String with a Char

。_饼干妹妹 提交于 2019-12-01 22:31:21
问题 Guys how do i compare a String with a char ? heres my code : private String s; private char c; public K(String string, char cc){ setS(string); setC(cc); } public void setS(String string){ this.s = string; } public void setC(char cc){ this.c = cc; } public boolean equals(K other){ return s.equals(c); } public boolean try(){ return s.equals(c); } if i call my method "try" it always returns me false even if i set both s = "s" and c = 's'. 回答1: The first thing I would say to any of my junior devs

how do I correctly override equals for inheritance in java?

ぐ巨炮叔叔 提交于 2019-12-01 21:36:27
I am using hibernate and id... is used for persistence (which is why it is omitted in comparison). (Also, using google guava helper equals) HolidayPackageVariant: public abstract class HolidayPackageVariant { private Integer idHolidayPackageVariant; private HolidayPackage holidayPackage; private String typeHolidayPackage; @Override public boolean equals(Object obj) { if (obj == this) return true; if(obj == null) return false; if (getClass().equals(obj.getClass())) { final HolidayPackageVariant otherPackageVariant = (HolidayPackageVariant) obj; return Objects.equal(getTypeHolidayPackage()

Question about jQuery source == on window

南楼画角 提交于 2019-12-01 20:54:43
data: function( elem, name, data ) { if ( !jQuery.acceptData( elem ) ) { return; } elem = elem == window ? windowData : elem; Copied directly from the jQuery source. Why is it not safe to use elem === window ? Why does jQuery use type coercion on the window object? It would appear that in IE there's an issue with top top == window // true top === window // false Raynos See here for why checking againts the window object with === is unsafe in IE. I think the root cause is that IE is closely coupled with the Windows OS so you have various OS objects referenced through window and the equality

Java 1.7 Override of hashCode() not behaving as I would expect

末鹿安然 提交于 2019-12-01 19:59:26
问题 I have a class where I have overridden both the hashCode method as well as the equals method. The equals method behaves as I would expect it to, however the hashCode method does not seem to behave as I would expect. I'm assuming therefor my expectation is incorrect, but not sure why. Below are the overridden methods: public class Car extends SomeBaseClass implements Cloneable, Serializable { private static final long serialVersionUID = 1L; private String id; private String name; private

Java 1.7 Override of hashCode() not behaving as I would expect

两盒软妹~` 提交于 2019-12-01 19:34:49
I have a class where I have overridden both the hashCode method as well as the equals method. The equals method behaves as I would expect it to, however the hashCode method does not seem to behave as I would expect. I'm assuming therefor my expectation is incorrect, but not sure why. Below are the overridden methods: public class Car extends SomeBaseClass implements Cloneable, Serializable { private static final long serialVersionUID = 1L; private String id; private String name; private String carName; private String carModel; private String displayTextCar; public boolean equals(Car car) {

To equals and hashcode or not on entity classes, that is the question

谁说我不能喝 提交于 2019-12-01 17:32:46
I have been trying to reason about the best way to handle whether it is generally good practice to implement hashcode and equals on entities (I mean entity in the general sense but in most cases it will be a JPA entity). In Chapter 24 of the Hibernate manual http://docs.jboss.org/hibernate/core/3.3/reference/en/html/best-practices.html it says this... Identify natural keys for all entities, and map them using . Implement equals() and hashCode() to compare the properties that make up the natural key. It makes sense to have .equals and .hashcode include only these natural keys but what if you

To equals and hashcode or not on entity classes, that is the question

允我心安 提交于 2019-12-01 16:30:30
问题 I have been trying to reason about the best way to handle whether it is generally good practice to implement hashcode and equals on entities (I mean entity in the general sense but in most cases it will be a JPA entity). In Chapter 24 of the Hibernate manual http://docs.jboss.org/hibernate/core/3.3/reference/en/html/best-practices.html it says this... Identify natural keys for all entities, and map them using . Implement equals() and hashCode() to compare the properties that make up the

Ignore case in string comparison

耗尽温柔 提交于 2019-12-01 15:54:11
If I have two variables, a and b and they could be integers, float, or strings. I want to return True if they are equal (in case of string, ignore case). As Pythonic as possible. This is the most pythonic I can think of. Better to ask for foregiveness than for permission: >>> def iequal(a, b): ... try: ... return a.upper() == b.upper() ... except AttributeError: ... return a == b ... >>> >>> iequal(2, 2) True >>> iequal(4, 2) False >>> iequal("joe", "Joe") True >>> iequal("joe", "Joel") False How about this, without isinstance (frowned upon): def equal(a, b): try: return a.lower() == b.lower()

Ignore case in string comparison

邮差的信 提交于 2019-12-01 15:47:02
问题 If I have two variables, a and b and they could be integers, float, or strings. I want to return True if they are equal (in case of string, ignore case). As Pythonic as possible. 回答1: This is the most pythonic I can think of. Better to ask for foregiveness than for permission: >>> def iequal(a, b): ... try: ... return a.upper() == b.upper() ... except AttributeError: ... return a == b ... >>> >>> iequal(2, 2) True >>> iequal(4, 2) False >>> iequal("joe", "Joe") True >>> iequal("joe", "Joel")