equals

Is there a complete IEquatable implementation reference?

女生的网名这么多〃 提交于 2019-11-26 17:57:50
问题 Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete. I want to find or write a definitive reference which must include: How to implement IEquatable correctly How to override Equals correctly How to override GetHashCode correctly How to implement the ToString method correctly How to implement the

Is it a bad idea if equals(null) throws NullPointerException instead?

旧城冷巷雨未停 提交于 2019-11-26 17:57:33
问题 The contract of equals with regards to null , is as follows: For any non-null reference value x , x.equals(null) should return false . This is rather peculiar, because if o1 != null and o2 == null , then we have: o1.equals(o2) // returns false o2.equals(o1) // throws NullPointerException The fact that o2.equals(o1) throws NullPointerException is a good thing, because it alerts us of programmer error. And yet, that error would not be catched if for various reasons we just switched it around to

Overloading operator== versus Equals()

旧巷老猫 提交于 2019-11-26 17:26:58
问题 I'm working on a C# project on which, until now, I've used immutable objects and factories to ensure that objects of type Foo can always be compared for equality with == . Foo objects can't be changed once created, and the factory always returns the same object for a given set of arguments. This works great, and throughout the code base we assume that == always works for checking equality. Now I need to add some functionality that introduces an edge case for which this won't always work. The

BigDecimal equals() versus compareTo()

江枫思渺然 提交于 2019-11-26 17:17:42
Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BigDecimal x = new BigDecimal("1"); BigDecimal y = new BigDecimal("1.00"); System.out.println(x.equals(y)); System.out.println(x.compareTo(y) == 0 ? "true": "false"); } } You can (consciously) say that x is equal to y (not object reference), but when you run the program, the following result shows: false true Question: What's the difference between compareTo() and equals() in

When does HashSet 'add' method calls equals? [duplicate]

女生的网名这么多〃 提交于 2019-11-26 16:43:37
This question already has an answer here: What issues should be considered when overriding equals and hashCode in Java? 11 answers I did this test in a HashSet comparision and equals is not being called I would like to consider equals when farAway=false (A function to check two point distances) Full compilable code, you could test it, and tells why equals is not being called in this example. public class TestClass{ static class Posicion { private int x; private int y; @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return

How to ensure hashCode() is consistent with equals()?

心不动则不痛 提交于 2019-11-26 16:37:42
When overriding the equals() function of java.lang.Object, the javadocs suggest that, it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. The hashCode() method must return a unique integer for each object (this is easy to do when comparing objects based on memory location, simply return the unique integer address of the object) How should a hashCode() method be overriden so that it returns a unique integer for each object based

java why should equals method input parameter be Object

ぐ巨炮叔叔 提交于 2019-11-26 16:35:55
问题 I'm going through a book on data structures. Currently I'm on graphs, and the below code is for the vertex part of the graph. class Vertex<E>{ //bunch of methods public boolean equals(Object o){ //some code } } When I try to implement this equals method my compiler complains about not checking the type of the parameter and just allowing any object to be sent it. It also does seem a bit strange to me why that parameter shouldn't be a Vertex instead of an Object. Is there a reason why the

Why can't we use '==' to compare two float or double numbers [duplicate]

跟風遠走 提交于 2019-11-26 16:34:36
This question already has an answer here: Test for floating point equality. (FE_FLOATING_POINT_EQUALITY) 3 answers Why does Double.NaN==Double.NaN return false? 9 answers I am reading Effective java by Joshua Bloch and in Item 8: Obey the general contract when overriding equals , this statement is written for float fields, use the Float.compare method; and for double fields, use Double.compare. The special treatment of float and double fields is made necessary by the existence of Float.NaN, -0.0f and the analogous double constants; Can someone explain me with example why we can't use == for

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

一个人想着一个人 提交于 2019-11-26 15:15:54
问题 I want to understand the scenarios where IEqualityComparer<T> and IEquatable<T> should be used. The MSDN documentation for both looks very similar. 回答1: 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. 回答2: 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

Is there a Java reflection utility to do a deep comparison of two objects?

别来无恙 提交于 2019-11-26 15:10:51
I'm trying to write unit tests for a variety of clone() operations inside a large project and I'm wondering if there is an existing class somewhere that is capable of taking two objects of the same type, doing a deep comparison, and saying if they're identical or not? Unitils has this functionality: Equality assertion through reflection, with different options like ignoring Java default/null values and ignoring order of collections I love this question! Mainly because it is hardly ever answered or answered badly. It's like nobody has figured it out yet. Virgin territory :) First off, don't