equals

Should the id field of a JPA entity be considered in equals and hashCode?

泪湿孤枕 提交于 2019-12-05 11:46:06
问题 I hit a problem when writing tests for a database application using JPA2 and EclipseLink: I add some entity to a database, retrieve it later and want to compare it to an instance which has the values I expect to confirm that the addition worked as I intended. First I wrote something like assertEquals(expResult, dbResult); which failed, because I can't really know the value of id field, which is generated by the database and therefore dbResult differs from expResult which I created with new

Technique for extending a class with private constructors

安稳与你 提交于 2019-12-05 11:15:29
Is there a standard technique to "extend" a class with private constructors, as with most Singleton classes? Specifcally, I'm trying to extend the java.lang.management.ThreadInfo class because I'm adding a LOT of them to a HashSet to control uniqueness. However, the way I determine if two threads are equal is different and not identical to the default implementation of the equals() method. Extending the class is obviously not an option in this case. Would it be reasonable to make something like a wrapper class which accepts a ThreadInfo in the constructor and then manually populates all

Java : “xx”.equals(variable) better than variable.equals(“xx”) , TRUE?

浪子不回头ぞ 提交于 2019-12-05 10:59:31
问题 I'm reviewing a manual of best practices and recommendation coding java I think is doubtful. Recomendation: String variable; "xx".equals(variable) // OK variable.equals("xx") //Not recomended Because prevents appearance of NullPointerException that are not controlled Is this true? 回答1: This is a very common technique that causes the test to return false if the variable is null instead of throwing a NullPointerException . But I guess I'll be different and say that I wouldn't regard this as a

Can .NET test arrays for equivalence and not just equal references?

冷暖自知 提交于 2019-12-05 08:58:57
var a = new double[] {1, 2, 3}; var b = new double[] {1, 2, 3}; System.Console.WriteLine(Equals(a, b)); // Returns false However, I'm looking for a way to compare arrays which would compare the internal values instead of refernces. Is there a built in way to do this in .NET? Also, while I understand Equals comparing references, GetHashCode returns different values for these two arrays also, which I feel shouldn't happen, since they have the same internal values. I believe you are looking for the Enumerable.SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method. var a = new

why is that str.count('') ≠ (from str.count('A') + str.count('B') + … + str.count('Z'))

霸气de小男生 提交于 2019-12-05 05:34:43
问题 It (should, to me,) say True if there are only vowels in the string(phrase); otherwise says False . I don't understand why it always will return False , since (x >= x) always returns True . I thank anyone for checking the solution to this query. (str) -> bool def valid_letter_sequence(abc): valid_letters = abc.count('A') + abc.count('E') + abc.count('I') + abc.count('O') + abc.count('U') counted_letters = abc.count('') if valid_letters >= counted_letters: return True else: return False 回答1:

Check if two std::function are Equal

泄露秘密 提交于 2019-12-05 03:28:43
If I have two std::function s, how can I check whether both hold the same function or not? Additional Information: I have a vector of functions std::vector<std::function<void()>> and before adding another function to the vector I want to check if it already is contained. I dont think that they both can be compared. Here is an example to explain some points on std::function comparison Generally std::function and boost::function are not comparable because they need their stored object be comparable but not all function objects and also lambdas have operator== so std or boost functions have no

ArrayList - add “same” objects (same => equals, hashCode), Threads

☆樱花仙子☆ 提交于 2019-12-05 02:16:23
Ive got one question. What happens when I try to add the "same" object twice to an ArrayList. With "the same" I mean an object of an individual class, which is identified as the same with equals() and hashCode(). It has different values for most of the member variables and was created from maybe different threads, but for equals() and hashCode() its the "same". Does the second object then replace the first object? Also, what happens if two threads try to add those objects exactly at the same time to the ArrayList? Is this even possible? If yes, what happens? Thank you! :-) [EDIT] Thanks for

Correctly synchronizing equals() in Java

孤街醉人 提交于 2019-12-05 01:10:48
I have the following class which contains only one field i . Access to this field is guarded by the lock of the object ("this"). When implementing equals() I need to lock this instance (a) and the other (b). If thread 1 calls a.equals(b) and at the same time thread 2 calls b.equals(a), the locking order is reverse in the two implementations and may result in deadlock. How should I implement equals() for a class which has synchronized fields? public class Sync { // @GuardedBy("this") private int i = 0; public synchronized int getI() {return i;} public synchronized void setI(int i) {this.i = i;}

Inconsistency in Equals and GetHashCode methods

百般思念 提交于 2019-12-05 00:25:49
After reading this question Why do "int" and "sbyte" GetHashCode functions generate different values? I wanted to dig further and found following behavior: sbyte i = 1; int j = 1; object.Equals(i, j) //false (1) object.Equals(j, i) //false (2) i.Equals(j) //false (3) j.Equals(i) //true (4) i == j //true (5) j == i //true (6) i.GetHashCode() == j.GetHashCode() //false (7) Difference between (3) and (4) breaks the requirement that Equals should be symmetric. Difference between (2) and (4) is not coherent with MSDN specification that says: If the two objects do not represent the same object

C# how to calculate hashcode from an object reference

Deadly 提交于 2019-12-04 23:22:21
Folks, here's a thorny problem for you! A part of the TickZoom system must collect instances of every type of object into a Dictionary<> type. It is imperative that their equality and hash code be based on the instance of the object which means reference equality instead of value equality. The challenge is that some of the objects in the system have overridden Equals() and GetHashCode() for use as value equality and their internal values will change over time. That means that their Equals and GetHashCode are useless. How to solve this generically rather than intrusively? So far, We created a