equals

How should we really be implenting Equals and GetHashCode for NHibernate entities

旧时模样 提交于 2019-12-01 03:14:58
There are many questions and answers and articles to this question available but in my opinion there seems to be no real clear/correct answer For me Ayende has the best generic implementation so far that I've seen : http://ayende.com/blog/2500/generic-entity-equality ....But it is from 2007 .... Is this the 'best way' to implement these methods especially with regard to NHibernate 3.2 which contains some differences in proxy implementation to earlier versions? Yes! You should be overriding Equals and GetHashCode . But, you shouldn't be doing value equality ( Name == other.Name && Age == other

How do I get unit test to run in java 7: java.lang.VerifyError: Expecting a stackmap frame at branch target

ⅰ亾dé卋堺 提交于 2019-12-01 02:54:47
Hi I am running a maven test using maven 3.0.3 with hibernate 4.0.0 Final release and spring 3.1 on jdk7 update 2. I get the following error. Caused by: java.lang.VerifyError: Expecting a stackmap frame at branch target 63 in method ${myDomainClass}.equals(Ljava/lang/Object;)Z at offset 24 at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2442) at java.lang.Class.getDeclaredMethods(Class.java:1808) at org.hibernate.property.BasicPropertyAccessor.getterMethod(BasicPropertyAccessor.java:352) at org.hibernate.property

Split list into two equal lists in F#

你离开我真会死。 提交于 2019-12-01 02:45:27
问题 I'm really new to F#, and I need a bit of help with an F# problem. I need to implement a cut function that splits a list in half so that the output would be... cut [1;2;3;4;5;6];; val it : int list * int list = ([1; 2; 3], [4; 5; 6]) I can assume that the length of the list is even. I'm also expected to define an auxiliary function gencut(n, xs) that cuts xs into two pieces, where n gives the size of the first piece: gencut(2, [1;3;4;2;7;0;9]);; val it : int list * int list = ([1; 3], [4; 2;

Overriding hashCode() - is this good enough?

狂风中的少年 提交于 2019-12-01 01:28:23
问题 For a class whose fields are solely primitive, ex.: class Foo { int a; String b; boolean c; long d; boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Foo)) return false; Foo other = (Foo) o; return a == other.a && b.equals(other.b) && c == other.c && d = other.d; } } Is this a reasonably "good enough" way to write hashCode() ? boolean hashCode() { return (b + a + c + d).hashCode(); } That is, I construct a String out of the same fields that equals() uses, and then just

Equality test of images using ImageMagick

久未见 提交于 2019-12-01 01:22:36
Is there any equality predicate function in ImageMagick library? I want to compare two images and find whether they are the exactly same (all colors of the pixels are the same) or have any differences. I’ve looked around, but it seems not to have a such function. Should I write the function using pixel iterators by myself? ImageMagick provides the compare function to properly compare images. Checking the md5 checksum of two images is not the correct approach, since some image formats (e.g. PNG and JPEG with EXIF for example), contain the date and time the file was created (see example 1) below

Result of calling IEquatable<T>.Equals(T obj) when this == null and obj == null?

自闭症网瘾萝莉.ら 提交于 2019-12-01 00:48:02
问题 What should IEquatable<T>.Equals(T obj) do when this == null and obj == null ? 1) This code is generated by F# compiler when implementing IEquatable<T> . You can see that it returns true when both objects are null : public sealed override bool Equals(T obj) { if (this == null) { return obj == null; } if (obj == null) { return false; } // Code when both this and obj are not null. } 2) Similar code can be found in the question "in IEquatable implementation is reference check necessary" or in

LinkedHashSet .equals() vs LinkedList .equals() with same elements but different order

拟墨画扇 提交于 2019-12-01 00:34:39
问题 Consider the following SSCCE: public static void main(String[] args) { LinkedHashSet<String> set1 = new LinkedHashSet<>(); set1.add("Bob"); set1.add("Tom"); set1.add("Sam"); LinkedHashSet<String> set2 = new LinkedHashSet<>(); set2.add("Sam"); set2.add("Bob"); set2.add("Tom"); System.out.println(set1); System.out.println(set2); System.out.println(set1.equals(set2)); } This prints: [Bob, Tom, Sam] [Sam, Bob, Tom] true Yet if you change LinkedHashSet to LinkedList : public static void main

Java之hashCode()和equals()方法

喜欢而已 提交于 2019-11-30 19:32:28
程序世界和现实世界其实是一样的, 相等 和 相同 是不同的概念,就此简要说明一下其中的含义: 1 equals() 的作用是什么? 2 equals() 与 == 的区别是什么? 3 hashCode() 的作用是什么? 4 hashCode() 和 equals() 之间有什么联系? 第1部分 equals() 的作用 equals() 的作用是 用来判断两个对象是否相等 。有5个很重要的特性: 1. 对称性:如果x.equals(y)返回是"true",那么y.equals(x)也应该返回是"true"。 2. 反射性:x.equals(x)必须返回是"true"。 3. 类推性:如果x.equals(y)返回是"true",而且y.equals(z)返回是"true",那么z.equals(x)也应该返回是"true"。 4. 一致性:如果x.equals(y)返回是"true",只要x和y内容一直不变,不管你重复x.equals(y)多少次,返回都是"true"。 5. 非空性,x.equals(null),永远返回是"false";x.equals(和x不同类型的对象)永远返回是"false"。 equals() 定义在JDK的Object.java中。通过判断 两个对象的 地址 是否相等(即,是否是同一个对象)来区分它们是否相等。源码如下: public boolean

Equality relations in Scala

这一生的挚爱 提交于 2019-11-30 19:26:46
I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some scala solution. The classic issue manifests itself in thinking about, say, a trade. Let's say I make two trades of +100 vodafone shares @150p. The two trades are equal, yes? Except they are not the same trade . In the case of a normal real-world system, with persistence or serialization, I cannot rely on identity to tell me whether two references

Updating Java HashMap key

有些话、适合烂在心里 提交于 2019-11-30 18:33:11
I was just wondering, what would happen if key of a HashMap is mutable, test program below demonstrate that and I am unable to understand when both equals and hashCode methods returns true and same value, why does hashmap.containsKey return false . public class MutableKeyHashMap { public static void main(String []a){ HashMap<Mutable, String> map = new HashMap<Mutable, String>(); Mutable m1 = new Mutable(5); map.put(m1, "m1"); Mutable m2 = new Mutable(5); System.out.println(map.containsKey(m2)); m2.setA(6); m1.setA(6); Mutable m3 = map.keySet().iterator().next(); System.out.println(map