equals

How to compare two object arrays in Java?

假装没事ソ 提交于 2019-11-27 02:50:55
问题 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

Should I write equals() and hashCode() methods in JPA entities?

删除回忆录丶 提交于 2019-11-27 02:49:10
I want to check if entity is in a Collection member ( @OneToMany or @ManyToMany ) of another entity: if (entity2.getEntities1().contains(entity1)) { } Not necessarily. There are three options: don't override - thus you will be working with instances. This is fine in cases when you are working with the collections with only entities that are attached to the session (and hence guaranteed to be the same instance). This is (for me) the preferred way in many cases, because it requires less code and less consideration when overriding override hashCode() and equals() with a business key. That may be

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

半城伤御伤魂 提交于 2019-11-27 02:47:43
问题 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. 回答1: 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

Java Hashset.contains() produces mysterious result

为君一笑 提交于 2019-11-27 02:46:03
问题 I don't usually code in Java, but recently I started not having a choice. I might have some major misunderstanding of how to properly use HashSet. So it might be possible something I did is just plain wrong. However I'm grateful for any help, you might offer. So the actual problem: In a small program I was writing, I was generating very similar objects, which, when created, would have a very specific id (a string or in my last iteration a long ). Because each object would spawn new objects, I

Check for equality in Spacebars?

半城伤御伤魂 提交于 2019-11-27 02:36:38
问题 I am trying to do what I think should be a very simple task, but have been failing to do so in the past hour. I want to select a select option by default if the user property matches the value. <select name="myName"> {{#each addKeys myTable}} <!-- addKeys creates variables for keys and values --> <option value="{{key}}" {{#if currentUser.property === key}}selected="selected"{{/if}}>{{value}}</option> {{/each}} </select> Now I thought this was straightforward enough to be implemented. But it

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

非 Y 不嫁゛ 提交于 2019-11-27 01:52:22
问题 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. 回答1:

java.lang.Comparable and equals

老子叫甜甜 提交于 2019-11-27 01:47:57
问题 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() ? 回答1: While it is

Why BigDecimal(“5.50”) not equals to BigDecimal(“5.5”) and how to work around this issue?

不羁岁月 提交于 2019-11-27 01:44:35
问题 Actually, I've found possible solution //returns true new BigDecimal("5.50").doubleValue() == new BigDecimal("5.5").doubleValue() Of course, it can be improved with something like Math.abs (v1 - v2) < EPS to make the comparison more robust, but the question is whether this technique acceptable or is there a better solution? If someone knows why java designers decided to implement BigDecimal's equals in that way, it would be interesting to read. 回答1: From the javadoc of BigDecimal equals

How to compare two maps by their values

有些话、适合烂在心里 提交于 2019-11-27 01:12:29
问题 How to compare two maps by their values? I have two maps containing equal values and want to compare them by their values. Here is an example: Map a = new HashMap(); a.put("foo", "bar"+"bar"); a.put("zoo", "bar"+"bar"); Map b = new HashMap(); b.put(new String("foo"), "bar"+"bar"); b.put(new String("zoo"), "bar"+"bar"); System.out.println("equals: " + a.equals(b)); // obviously false How should I change the code to obtain a true? 回答1: Your attempts to construct different strings using

Equals(item, null) or item == null

不打扰是莪最后的温柔 提交于 2019-11-27 00:52:08
问题 Is code that uses the static Object.Equals to check for null more robust than code that uses the == operator or regular Object.Equals? Aren't the latter two vulnerable to being overridden in such a way that checking for null doesn't work as expected (e.g. returning false when the compared value is null)? In other words, is this: if (Equals(item, null)) { /* Do Something */ } more robust than this: if (item == null) { /* Do Something */ } I personally find the latter syntax easier to read.