equals

Understanding the workings of equals and hashCode in a HashMap

I have this test code: import java.util.*; class MapEQ { public static void main(String[] args) { Map<ToDos, String> m = new HashMap<ToDos, String>(); ToDos t1 = new ToDos("Monday"); ToDos t2 = new ToDos("Monday"); ToDos t3 = new ToDos("Tuesday"); m.put(t1, "doLaundry"); m.put(t2, "payBills"); m.put(t3, "cleanAt...

Inconsistency on equals() applied to Vector

Vector <Double> x = new Vector<Double>(); Vector <Integer> y = new Vector <Integer>(); System.out.print(x.equals(y)); This prints: true Why? Isn't equals() -by default- supposed to compare if two references point to the same object? ...

.NET Frustration - Process.GetProcessById Returns New Reference

I'm writing a c# program that will launch many child processes. At some time later on, I'll need to retrieve those processes by ID and then match those processes to a set of processes stored in a Dictionary that were added to the Dictionary when they were first created. However, I'm running into a problem that seems like pure ridiculou...

To equals and hashcode or not on entity classes, that is the question.

I have been trying to reason about the best way to handle whether it is generally good practice to implement hashcode and equals on entities (I mean entity in the general sense but in most cases it will be a JPA entity). In Chapter 24 of the Hibernate manual http://docs.jboss.org/hibernate/core/3.3/reference/en/html/best-practices.html ...

Linq 'equals' keyword Revisited - Does it compare values and references to objects?

This question is a follow on to link text hence the revisited in the title. I raise this as a new question as the accepted answer and comment made under the original question both suggest that the equals keyword used in the join query refers to only value types within the comparison. I believe this is misleading as follows. Behind the s...

Scala semantics of equals/hashCode for case classes with traits

I am a newcomer to Scala. In 2.7.7, the following code abstract class C case class CC() extends C trait T val c1 = CC() val c2 = new CC() with T println(c1.hashCode == c2.hashCode,c1 equals c2) prints (false,true) whereas I would have expected (false,false) What am I missing? Thanks in advance. ...

Overriding equals() & hashCode() in sub classes ... considering super fields

Is there a specific rule on how Overriding equals() & hashCode() in sub classes considering super fields ?? knowing that there is many parameters : super fields are private/public , with/without getter ... For instance, Netbeans generated equals() & hashCode() will not consider the super fields ... and new HomoSapiens("M", "80", "1...

Getting KeyNotFoundException when using key previously retrieved from key collection??

I've got the following code where for some reason I'm getting a KeyNotFoundException even though I'm using a key that I had retrived a couple of lines above. Does anyone know of a situation where this wouldn't work? I'm stumped. BTW 'SchemaElementType is an enum. public class DefaultValue { private Dictionary<Parameter, string> _para...

How to implement hashCode and equals method

How should I implement hashCode() and equals() for the following class in Java? class Emp { int empid ; // unique across all the departments String name; String dept_name ; String code ; // unique for the department } ...

equals issue with adding components to JPanel?

I am making a card game where the cards are represented as JLabels, having names and some other attributes which are changed during the game. I want to define equality as simply comparing the full names (represented by a field fullName) of two cards. However, this gives me the problem that I can't add the same card twice (because I have ...

Does the StringBuffer equals method compare content?

Possible Duplicate: Comparing StringBuffer content with equals StringBuffer s1= new StringBuffer("Test"); StringBuffer s2 = new StringBuffer("Test"); if(s1.equals(s2)) { System.out.println("True"); } else { System.out.println("False"); } Why does that code print "False"? ...

MS Access 2003 - What is the equal function in code?

What is the code for an equal function? This values in this column and the values in this column and the values in this column all equal this column. ...

Java: clean way to automatically throw UnsupportedOperationException when calling hashCode() and equals()?

We've got an OO codebase where in quite a lot of cases hashcode() and equals() simply don't work, mostly for the following reason: There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction. Tha...

Using equals inside a generic class

Hi, I'd like my EqualTester generic class to call the overridden equals(...) method of its generic parameter, but it seems to call Object.equals instead. Here is my test code: import junit.framework.TestCase; public class EqualityInsideGenerics extends TestCase { public static class EqualTester<V> { public boolean check(...

Java Problem implementing clone, generic class.

I have a class that implements a 2Dtable. The elements in the table is generic. And the table is stored like this: public Object[][] list; The problem is that calling clone on this apparently doesn't work. Note that my testcase initializes the table to store normal Integers. Tabell2D<Integer> en = new Tabell2D<Integer>(5,5); ...

Overloaded operator is never called in C++

I'm writing a math library as a practical exercise. I've run into some problems when overloading the = operator. When I debuged it, I noticed that the call to vertex1 = vertex2 calls the copy constructor instead. In the header file I have: //constructors vector3(); vector3( vector3 &v ); vector3(float ix, float iy, float iz); //opera...

Is there a BASIC dialect which uses "==" as the comparison operator?

Anyone who grew up on BASIC, and later switched to another language, had a real difficulty getting used to "(a == b)" rather than "(a = b)" to test for equality. Is there a dialect of BASIC which uses the "==" operator for comparisons rather than overloading "=" for assignments and comparisons? Or - and maybe this is stretching it - is ...

Why do I need to override the equals and hashcode method in java?

Recently I read through this Developer Works Document. The document is all about defining hashCode() and equals() effectively and correctly, but I am not able to figure out why we need to override these two methods. How can I take the decision to implement these method efficiently? ...

What is the better approach to compare a collection with another (architecturely speaking) ?

Here's an example of the architecture approach I favorited as for now: public abstract class CollectionComparer { public virtual SetEqual(IEnumerable enum1, IEnumerable enum2) { if(enum1== null && enum2== null) return true; if(enum1== null && !(enum2== null)) return false; if(!(enum1...

How to deal with NHibernate parent\child relationship with childs in ISet and generated IDs?

Having entered the world of NHibernate less than one year ago, I'm still developing my "personal" best practice and architectural solutions in this area... now I'm facing a pretty simple issue on which I'd like to have opinions or suggestions by somebody with more expertise. The scenario is the following: A straight parent/child relatio...