equals

Why .equals method is failing on two same value objects?

断了今生、忘了曾经 提交于 2019-11-28 09:23:19
问题 I have created a value object MarketVO and two instances of this value object have same elements and same value for each element. My value object class is: public class MarketVO { private double floatAmt; private Date marketDate; private long marketCap; } Here are the values: returnedData: FloatAmt: 247657.5418618201, MarketCap: 5249164, MarketDate: 2011-07-29 00:00:00.0 expectedData: FloatAmt: 247657.5418618201, MarketCap: 5249164, MarketDate: 2011-07-29 00:00:00.0 Now in my unit test class,

Strange behaviour of the Array type with `==` operator

人走茶凉 提交于 2019-11-28 09:17:17
scala> List(1,2,3) == List(1,2,3) res2: Boolean = true scala> Map(1 -> "Olle") == Map(1 -> "Olle") res3: Boolean = true But when trying to do the same with Array, it does not work the same. Why? scala> Array('a','b') == Array('a','b') res4: Boolean = false I have used 2.8.0.RC7 and 2.8.0.Beta1-prerelease. Because the definition of "equals" for Arrays is that they refer to the same array. This is consistent with Java's array equality, using Object.Equals , so it compares references. If you want to check pairwise elements, then use sameElements Array('a','b').sameElements(Array('a','b')) or

How to compare two object arrays in Java?

做~自己de王妃 提交于 2019-11-28 09:12:38
I have two object arrays like so: Object[] array1 = {0, 1, 2, 3}; Object[] array2 = {0, 1, 2, 3}; I would like to know if the arrays are equal. I'm defining equal as every value in array1 is the same as the value in that position in the array2. So these two arrays would be equal. What is the best why to find out if these two arrays are equal? if(array1 == array2) is not a deep equals so that won't work and I don't know if looping over each element and comparing them is the best and most efficient way to solve this problem. Does anyone have any better suggestions? Edit: I needed an equals that

java.lang.Comparable and equals

懵懂的女人 提交于 2019-11-28 08:59:09
If I implement java.lang.Comparable for a class, do I still have to override the equals() method? Or will the Comparable work for equals as well? If the answer is no , then what if some discrepancy arises? Let's say the way I term two objects as equal within the equals() method is different from the way I term two objects of the same class as equal within the compareTo() of the Comparable . Moreover, if I implement Comparable , do I also have to override equals() ? While it is recommended (and pretty sensible) that having a.compareTo(b) == 0 imply that a.equals(b) (and visa versa), it is not

A Mechanism for having different equals (physical equals and logical equals) on objects in Collection

柔情痞子 提交于 2019-11-28 08:37:16
问题 Is there any Equalator mechanism like Comparator so I can have different equals for coparing lists? EDIT: My goal is to differentiate between current list1.equals(list2) which checks if its a shallow copy or also a deep copy with all objects a.equals(b) and list1.identical(list2) which checks if its simply shallow copy with unmodified listing All these lists are from the same model. Some are copies of themselves so they hold the pointer to same objects, and others are deep copies so hierarchy

What code is generated for an equals/hashCode method of a case class?

妖精的绣舞 提交于 2019-11-28 08:09:30
I have some Java code which I'm translating to Scala. The code consists of some immutable classes which would fit the purpose of a case class in Scala. But I don't want to introduce bugs, therefore I want to be sure that the code being generated for equals and hashCode is/behaves equivalent to the current implementation. I already looked in "Programming in Scala" but it only says Third, the compiler adds “natural” implementations of methods toString, hashCode, and equals to your class. Scala has a compiler option -Xprint:typer , which you can use to get the "post-typing source code that it

How does equals() method work in Java [duplicate]

寵の児 提交于 2019-11-28 08:06:03
问题 This question already has an answer here: What is the difference between == and equals() in Java? 23 answers The equals method compares whether two object values are equal or not. My question is how it compares the two objects? How can it tell the two objects are equal or not? I want to know based on what it compares the two objects. I am not including the hashCode method. 回答1: The default implementation, the one of the class java.lang.Object , simply tests the references are to the same

Can you compare chars with ==? [duplicate]

℡╲_俬逩灬. 提交于 2019-11-28 07:44:07
问题 This question already has answers here : What is the difference between == and equals() in Java? (23 answers) Closed last year . For Strings you have to use equals to compare them, because == only compares the references. Does it give the expected result if I compare chars with == ? I have seen similar questions on stackoverflow, E.g. What is the difference between == vs equals() in Java? However, I haven't seen one that asks about using == on chars. 回答1: Yes, char is just like any other

Best practices regarding equals: to overload or not to overload?

笑着哭i 提交于 2019-11-28 07:20:23
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[] args) { class Thing { final int x; Thing(int x) { this.x = x; } public int hashCode() { return x; } public boolean equals(Thing other) { return this.x == other.x; } } List<Thing> myThings = Arrays.asList(new Thing(42)); System.out.println(myThings.contains(new Thing(42))); // prints "false" } } Note that contains returns false !!! We seems to have lost our things!! The bug, of course, is the fact that we've accidentally overloaded , instead of overridden , Object.equals(Object) .

XPath operator “!=”. How does it work?

时光毁灭记忆、已成空白 提交于 2019-11-28 07:15:26
XML document: <doc> <A> <Node>Hello!</Node> </A> <B> <Node/> </B> <C> </C> <D/> </doc> How would you evaluate the following XPath queries? /doc/A/Node != 'abcd' /doc/B/Node != 'abcd' /doc/C/Node != 'abcd' /doc/D/Node != 'abcd' I would expect ALL of these to evaluate to true . However, here are the results: /doc/A/Node != 'abcd' true /doc/B/Node != 'abcd' true /doc/C/Node != 'abcd' false /doc/D/Node != 'abcd' false Is this expected behavior? Or is it a bug with my XPath provider (jaxen)? Recommendation : Never use the != operator to compare inequality where one or both arguments are node-sets.