Why are two AtomicIntegers never equal?

This is partly because an AtomicInteger is not a general purpose replacement for an Integer. The java.util.concurrent.atomic package summary states: Atomic classes are not general purpose replacements for java.lang.Integer and related classes. They do not define methods such as hashCode and compareTo. (Because atomic variables are expected to be mutated, they are poor choices for … Read more

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

Apache commons-collections has CollectionUtils#isEqualCollection: Returns true if the given Collections contain exactly the same elements with exactly the same cardinality. That is, if the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b. Which is, I think, exactly what you’re after.

Difference between == operator and Equals() method in C#?

There are several things going on. Firstly, in this example: string s1 = “a”; string s2 = “a”; Console.WriteLine(s1 == s2); You claim that: Both are different object reference. That’s not true due to string interning. s1 and s2 are references to the same object. The C# specification guarantees that – from section 2.4.4.5 of … Read more

How to implement hashCode and equals method

in Eclipse right mouse click-> source -> generate hashCode() and equals() gives this: /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (code == null ? 0 : code.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ … Read more

Is there a complete IEquatable implementation reference?

Implementing IEquatable<T> for a Value Type Implementing IEquatable<T> for a value type is a little bit different than for a reference type. Let’s assume we have the Implement-Your-Own-Value-Type archetype, a Complex number struct. public struct Complex { public double RealPart { get; set; } public double ImaginaryPart { get; set; } } Our first step … Read more

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

Nice question, the surprising thing about the code above is that println “${‘test’}”.equals(‘test’) returns false. The other line of code returns the expected result, so let’s forget about that. Summary “${‘test’}”.equals(‘test’) The object that equals is called on is of type GStringImpl whereas ‘test’ is of type String, so they are not considered equal. But … Read more

tech