All Questions

617
votes
11answers
541626 views

What issues should be considered when overriding equals and hashCode in Java?

What issues / pitfalls must be considered when overriding equals and hashCode?
1127
votes
16answers
144420 views

What is the best algorithm for an overridden System.Object.GetHashCode?

In .NET System.Object.GetHashCode method is used in a lot of places, throughout the .NET base class libraries. Especially when finding items in a collection fast or to determine equality. Is there a ...
415
votes
46answers
323947 views

How to determine equality for two JavaScript objects?

A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java? Stack Overflow question Is the...
1071
votes
12answers
275639 views

Why is it important to override GetHashCode when Equals method is overridden?

Given the following class public class Foo { public int FooId { get; set; } public string FooName { get; set; } public override bool Equals(object obj) { Foo fooItem = obj as...
238
votes
27answers
272061 views

Why do I need to override the equals and hashCode methods 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 th...
837
votes
31answers
851445 views

How can I generate an MD5 hash?

Is there any method to generate MD5 hash of a string in Java?
171
votes
16answers
165481 views

How does a Java HashMap handle different objects with the same hash code?

As per my understanding I think: It is perfectly legal for two objects to have the same hashcode. If two objects are equal (using the equals() method) then they have the same hashcode. If two object...
238
votes
20answers
187457 views

Best implementation for hashCode method

How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ?
138
votes
8answers
49565 views

Why use a prime number in hashCode?

I was just wondering why is that primes are used in a class's hashCode() method? For example, when using Eclipse to generate my hashCode() method there is always the prime number 31 used: public int ...
38
votes
7answers
14398 views

Are mutable hashmap keys a dangerous practice?

Is it bad practice to use mutable objects as Hashmap keys? What happens when you try to retrieve a value from a Hashmap using a key that has been modified enough to change its hashcode? For example,...

Previous Next