equals

Does c# have the same issues like Java with equals and gethashcode() ?

Does c# have the same issues like Java with equals and gethashcode? issues like: http://onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html ...

How to compare two maps by their values

How to compare two maps by their values? I have two maps containing equal values and want to compare them by their values. Here is an example: Map a = new HashMap(); a.put("foo", "bar"+"bar"); a.put("zoo", "bar"+"bar"); Map b = new HashMap(); b.put(new String("foo"), "bar"+"bar"); b.put(new String("zoo"), "bar"+...

Consistent Equals() results, but inconsistent TreeMap.containsKey() result

I have the following object Node: private class Node implements Comparable<Node>(){ private String guid(); ... public boolean equals(Node o){ return (this == o); } public int hashCode(){ return guid.hashCode(); } public int compareTo(Node o...

Good resource for explaining how hash codes are used in collections

Hi, Can anyone give a good explanation and / or links to a good resource of how hash codes are used in storing and retrieving objects in hashtables, dictionaries etc, specifically in C# / .NET. I'm interested to see how Equals and GetHashCode are used collectively when storing and retrieving items. ...

Java - abstract class, equals(), and two subclasses

Hello, I have an abstract class named Xpto and two subclasses that extend it named Person and Car. I have also a class named Test with main() and a method foo() that verifies if two persons or cars (or any object of a class that extends Xpto) are equals. Thus, I redefined equals() in both Person and Car classes. Two persons are equal wh...

Java - is there a "subclassof" like instanceof?

Hello, Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance! ...

Why should I override hashCode() when I override equals() method?

Ok, I have heard from many places and sources that whenever I override the equals() method, I need to override the hashCode() method as well. But consider the following piece of code package test; public class MyCustomObject { int intVal1; int intVal2; public MyCustomObject(int val1, int val2){ intVal1 = val1; ...

Example of ==, equals and hashcode in java

Given this: String s1= new String("abc"); String s2= new String("abc"); String s3 ="abc"; System.out.println(s1==s3); System.out.println(s1==s2); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); Output is: ...

What's the difference between IEquatable and just overriding Object.Equals() ?

I want my Food class to be able to test whenever it is equal to another class. I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable or just override Object.Equals()? From MSDN: This method determines equality by using the default equality comparer, as defined by the objec...

What's the reason both Image and Bitmap classes don't implement a custom equality/hashcode logic?

From MSDN documentation, it seems as both GetHashCode() and Equals() haven't been overriden in Bitmap. Neither have them been overriden in Image. So both classes are using the Object's version of them just compares references. I wasn't too convinced so I decided to fire up Reflector to check it out. It seems MSDN is correct in that matte...

NHibernate set : Should I override Equals and GetHashCode ?

I am new to NHibernate. I am using <set ... > mapping for some many-to-one and many-to-many associations. These are exposed as properties of type ICollection<T>, in practice implemented by HashSet<T>. My question is, should I override Equals and GetHashCode for the related types, so they match the database identity of the types (in pra...

Overriding equals method without breaking symmetry in a class that has a primary key

Hi, the answer to this question is probably "not possible", but let me ask regardless :) Assuming we have a very simple JAVA class that has a primary key, for example: class Person { String ssid; String name; String address; ... } Now, I want to store people in a collection, meaning I will have to override the equals...

Java: If I overwrite the .equals method, can I still test for reference equality with ==?

I have the following situation: I need to sort trees based by height, so I made the Tree's comparable using the height attribute. However, I was also told to overwrite the equals and hashCode methods to avoid unpredictable behaviour. Still, sometimes I may want to compare the references of the roots or something along those lines using ...

overload Equals, is this wrong?

Reading some piece of code and I keep seeing this : public override bool Equals (object obj) { if (obj == null || this.GetType ().Equals (obj.GetType())) return false; //compare code... } Shouldn't it be like this (note the !): public override bool Equals (object obj) { if (obj == null || !this.GetType ().Equals (obj.G...

Collection, which method is used to authorize an add of an element ?

We find a lot of concrete subclasses under Collection. While trying to add an element in a concrete collection, this collection will use a method to determine if it can accept to store the element (and eventually that this element is not already in the collection). It could use equals(), hashCode() or compareTo() of the element. Is it ...

Is it a bad idea if equals(null) throws NullPointerException instead?

The contract of equals with regards to null, is as follows: For any non-null reference value x, x.equals(null) should return false. This is rather peculiar, because if o1 != null and o2 == null, then we have: o1.equals(o2) // returns false o2.equals(o1) // throws NullPointerException The fact that o2.equals(o1) throws NullPointe...

c# xml function to check whether a string is equal to a xml attribute, to add selected combobox item to textbox

i want to check the combobox.selecteditem.tostring() on combobox select in a given xml with several nodes, where each one has an attribute called "name" private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { try { textBox1.AppendText(nameAttributeCheck(comboBox1.SelectedItem.ToString())); } cat...

((System.Object)p == null)

Why do this: // If parameter cannot be cast to Point return false. TwoDPoint p = obj as TwoDPoint; if ((System.Object)p == null) { return false; } Instead of this: // If parameter cannot be cast to Point return false. TwoDPoint p = obj as TwoDPoint; if (p == null) { return false; ...

Best practices regarding equals: to overload or not to overload?

Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[] args) { class Thing { final int x; Thing(int x) { this.x = x; } public int hashCode() { return x; } public boolean equals(Thing other) { return this.x ==...

Why do contains()/indexOf() in Java collections use o.equals(e) and not e.equals(o)?

Why are the methods contains() and indexOf() in the Java collections framework defined using o.equals(e) and not e.equals(o) (where o is the argument of the methods and e is the element in the collection)? Anyone know the reasons of that? ...