equals

Using alternative comparison in HashSet

I stumbled across this problem when creating a HashSet[Array[Byte]] to use in a kind of HatTrie. Apparently, the standard equals() method on arrays checks for identity. How can I provide the HashSet with an alternative Comparator that uses .deepEquals() for checking if an element is contained in the set? Basically, I want this test to...

Why are these == but not `equals()`?

I'm a bit confused about the way Java treats == and equals() when it comes to int, Integer and other types of numbers. For example: Integer X = 9000; int x = 9000; Short Y = 9000; short y = 9000; List<Boolean> results = new ArrayList<Boolean>(); // results.add(X == Y); DOES NOT COMPILE 1) results.add(Y == 9000); ...

Is there a complete IEquatable implementation reference?

Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete. I want to find or write a definitive reference which must include: How to implement IEqu...

Hashsets and different instances of class with all members identical

Let's say I have a class called myclass. In my code I have two instances of myclass, myclass1 and myclass2. Everything about them is (public and private) properties are identical. If I try to add both of them to a HashSet will it add both or only one? If it adds both and I don't want it to, can I overidde equals in the myclass definiti...

Does Scala's BigDecimal violate the equals/hashCode contract?

As the Ordered trait demands, the equals method on Scala's BigDecimal class is consistent with the ordering. However, the hashcode is simply taken from the wrapped java.math.BigDecimal and is therefore inconsistent with equals. object DecTest { def main(args: Array[String]) { val d1 = BigDecimal("2") val d2 = BigDecimal("2.00"...

Compare two Java objects with a check for null

I feel like such a novice for asking this question, but is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this: public static boolean equals(Object o1, Object o2) { if (o1 == null) { return o2 == null; // Two nulls are considered equal } else if (o2 == null...

Accessing member variables of other object of same class in Ruby

In Java I can do: public boolean equals(Object other) { return this.aPrivateVariable == ((MyClass)other).aPrivateVariable; } This allows me to define equality without breaking the encapsulation of my class. How can I do the same in Ruby? Thanks. ...

Is there a Java reflection utility to do a deep comparison of two objects?

I'm trying to write unit tests for a variety of clone() operations inside a large project and I'm wondering if there is an existing class somewhere that is capable of taking two objects of the same type, doing a deep comparison, and saying if they're identical or not? ...

C#: How does the static object.Equals check for equality?

Say you have two different classes where each have their own implementation of Equals; which one is used? What if only one of them have one? Or none of them? Are any of the following lines equivalent? object .Equals( first, second ) first .Equals( second ) second .Equals( first ) I'm guessing that the first two might be equivalent, bu...

How do I setup a call of an Equals with a specific type overriding the Equals in MoQ?

Working with the fine mocking-framework MoQ, I came across a somewhat surprising facet (and I don't like surprises). I'm mocking out a class which should be added to a collection after a method-call, like so: public class SomeClass{ } public class Container { private List<SomeClass> classes = new List<SomeClass>(); public IEnumerab...

Maintain equal height on resize (jQuery)

I use this code for equalizing columns: jQuery.fn.equalHeight=function() { var maxHeight=0; this.each(function(){ if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;} }); this.each(function(){ $(this).height(maxHeight + "px"); if (this.offsetHeight>maxHeight) { $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px...

A line of java code and what it does?

So I have purchased the book "Java for Dummies" 4th Edition, and I must say it is probably the best 30 dollars I have ever spent on a book. I'm not new to coding, am actually fairly decent at at it if I say so myself. However, I've come across a line of code that has me a touch confused: public void setName(String n) { if(!n.equa...

Java SortedSet + Comparator, consistency with equals() question

I'd like to have a SortedSet of Collections (Sets themselves, in this case, but not necessarily in general), that sorts by Collection size. This seems to violate the proscription to have the Comparator be consistent with equals() - i.e., two collections may be unequal (by having different elements), but compare to the same value (becaus...

Overriding equals and hashCode methods for a JavaBeans implemented in Scala

Hello, I'm working on a project using iBatis and a Java caching tool ehcache but I'm implementing the model classes in Scala. I'm having a stong feeling that I'll have to override the equals and hashCode methods to enable the cache easily manage objects on it. Since most of the required properties in the scala classes are vars, I need ...

Object.Equals(obj1, obj2) vs obj1.Equals(obj2)?

Assuming both objects are not value types and both represent types which have overridden the Equals(...) method, is there any functional difference between: Calling obj1.Equals(obj2) Calling Object.Equals(obj1, obj2) ...or are they functionally the same? The Apress book I'm reading (Pro C# 2008), which is actually quite good, refers ...

Java: Always override equals?

When writing one's own classes, is it always necessary to override equals(Object o)? If I don't, will it automatically check that all the fields are the same? Or does it just check if the two variables point to the same object? ...

Java Strings: compareTo() vs. equals()

When testing for equality of strings in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had been taught to use compareTo() == 0 instead of equals(). This feels unnatural (as compareTo()...

GetHashCode issue

Hi, Can anyone help explain the following. I am having an issue with a Dictionary where ContainsKey evaluates to false while both Equals and GetHashCode for the objects are successful. Below is the output from the immediate window in Visual Studio: ?LocationToRackingGroup.Keys.ToArray()[23].Equals(location) true ?LocationToRackingGroup...

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

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...

An efficient equals(Object o) implementation

I read this SO post after I wrote out the title but still decided to go through with the question on bug-proof implementations of equals in Java. This is my normal implementation @Override public boolean equals(Object o){ if(o == null) return false; if(o instanceof CompositePk == false) return false; ...