equals

Create the perfect JPA entity [closed]

一世执手 提交于 2019-11-26 14:57:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I've been working with JPA (implementation Hibernate) for some time now and each time I need to create entities I find myself struggling with issues as AccessType, immutable properties, equals/hashCode, ... . So I decided to try and find out the general best practice for each

How to quickly check if two data transfer objects have equal properties in C#?

心已入冬 提交于 2019-11-26 14:15:40
I have these data transfer objects: public class Report { public int Id { get; set; } public int ProjectId { get; set; } //and so on for many, many properties. } I don't want to write public bool areEqual(Report a, Report b) { if (a.Id != b.Id) return false; if (a.ProjectId != b.ProjectId) return false; //Repeat ad nauseum return true; } Is there a faster way to test if two object with only properties have the same values (something that doesn't require one line of code or one logical expression per property?) Switching to structs is not an option. How about some reflection, perhaps using

Is there a way to check if two Collections contain the same elements, independent of order?

被刻印的时光 ゝ 提交于 2019-11-26 13:49:25
问题 I've been looking for a method that operates like Arrays.equals(a1, a2) , but ignoring the element order. I haven't been able to find it in either Google Collections (something like Iterables.elementsEqual() , but that does account for ordering) and JUnit ( assertEquals() obviously just calls equals() on the Collection, which depends on the Collection implementation, and that's not what I want) It would be best if such a method would take Iterable s, but I'm also fine with simply taking

String comparison and String interning in Java

北城余情 提交于 2019-11-26 13:47:38
When should one compare String s as objects and when should one use their equals method? To make sure, I always use equals , but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use? Thanks! You should almost always use equals . You can be certain that string1 == string2 will work if: You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for some other reason) You know you're dealing with compile-time string constants You've manually interned the

Groovy different results on using equals() and == on a GStringImpl

吃可爱长大的小学妹 提交于 2019-11-26 13:22:38
问题 According to the groovy docs, the == is just a 'clever' equals() as it also takes care of avoiding NullPointerException. So, the == and equals() should return the same value if the objects are not null. However, I'm getting unexpected results on executing the following script: println "${'test'}" == 'test' println "${'test'}".equals('test') The output that I'm getting is true false An example of this can be found here. Is this a known bug related to GStringImpl or something that I'm missing?

What's the best way to compare Double and Int?

二次信任 提交于 2019-11-26 12:47:06
问题 The following code in C# doesn\'t work: int iValue = 0; double dValue = 0.0; bool isEqual = iValue.Equals(dValue); So, the question: what\'s the best way to compare Double and Int? 回答1: You really can't compare floating point and integral values in a naive way; particularly, since there's the classic floating point representation challenges. What you can do is subtract one from the other and see if the difference between them is less than some precision you care about, like so: int iValue = 0

What does comparison being consistent with equals mean ? What can possibly happen if my class doesn't follow this principle?

烂漫一生 提交于 2019-11-26 12:43:38
问题 From the JavaDoc of TreeMap : Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are

Is it possible in java make something like Comparator but for implementing custom equals() and hashCode()

对着背影说爱祢 提交于 2019-11-26 12:16:12
问题 I have an array of objects and I want to concatenate it with another array of objects, except that objects that have same id\'s. That objects are used in many places in the system and don\'t have hash code or equals implemented. So I don\'t want to implement hashCode() and equals() , cause I\'m afraid to break something somewhere in the system where that objects are used and I don\'t know about that. I want to put all that objects in a set, but somehow make the objects use custom hashCode()

How should equals and hashcode be implemented when using JPA and Hibernate

社会主义新天地 提交于 2019-11-26 12:02:49
How should model class's equals and hashcode be implemented in Hibernate? What are the common pitfalls? Is the default implementation good enough for most cases? Is there any sense to use business keys? It seems to me that it's pretty hard to get it right to work in every situation, when lazy fetching, id generation, proxy, etc are taken into account. ChssPly76 Hibernate has a nice and long description of when / how to override equals() / hashCode() in documentation The gist of it is you only need to worry about it if your entity will be part of a Set or if you're going to be detaching /

When “” == s is false but “”.equals( s ) is true

余生颓废 提交于 2019-11-26 11:56:14
问题 EDIT Thanks for the prompt responses. Please see what the real question is. I have made it bold this time. I do understand the difference between == and .equals. So, that\'s not my question (I actually added some context for that) I\'m performing the validation below for empty strings: if( \"\" == value ) { // is empty string } In the past when fetching values from the db or deserializing objects from another node, this test failed , because the two string instances were indeed different