Questions tagged [hashcode]

A hash code is a result of applying a hash function to data, usually resulting in an integer.

2025 questions
1666
votes
22 answers

What is the best algorithm for overriding GetHashCode?

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when determining equality. Is there a standard algorithm…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
1649
votes
15 answers

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 Foo; if (fooItem == null) { …
David Basarab
  • 72,212
  • 42
  • 129
  • 156
1090
votes
34 answers

How can I generate an MD5 hash in Java?

Is there any method to generate MD5 hash of a string in Java?
Akshay
  • 11,803
  • 5
  • 29
  • 26
965
votes
82 answers

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 there any kind of hashCode function in JavaScript? is…
user4903
617
votes
11 answers

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

What issues / pitfalls must be considered when overriding equals and hashCode?
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
526
votes
31 answers

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, however I am not able to figure out why we need to override these two methods. How can I take the…
Shashi
  • 12,487
  • 17
  • 65
  • 111
325
votes
20 answers

Best implementation for hashCode method for a collection

How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ?
Omnipotent
  • 27,619
  • 12
  • 30
  • 34
280
votes
11 answers

How to get the unique ID of an object which overrides hashCode()?

When a class in Java doesn't override hashCode(), printing an instance of this class gives a nice unique number. The Javadoc of Object says about hashCode(): As much as is reasonably practical, the hashCode method defined by class Object does…
ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100
266
votes
15 answers

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 objects are not equal then they cannot have the same…
akshay
  • 5,235
  • 14
  • 39
  • 49
255
votes
8 answers

What's a correct and good way to implement __hash__()?

What's a correct and good way to implement __hash__()? I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries. As __hash__() returns an integer and is used for "binning" objects…
user229898
  • 3,057
  • 3
  • 19
  • 9
203
votes
9 answers

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 hashCode() { final int prime = 31; …
Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
192
votes
14 answers

Good Hash Function for Strings

I'm trying to think up a good hash function for strings. And I was thinking it might be a good idea to sum up the unicode values for the first five characters in the string (assuming it has five, otherwise stop where it ends). Would that be a good…
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
188
votes
8 answers

What is the use of hashCode in Java?

In Java, obj.hashCode() returns some value. What is the use of this hash code in programming?
Vinoth Kumar
  • 3,243
  • 7
  • 24
  • 13
175
votes
20 answers

Is there hash code function accepting any object type?

Basically, I'm trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as, set[obj] = true; This works, up to a point. It works great with string and…
Boog
  • 3,744
  • 4
  • 28
  • 25
169
votes
9 answers

Memory address of variables in Java

Please take a look at the picture below. When we create an object in java with the new keyword, we are getting a memory address from the OS. When we write out.println(objName) we can see a "special" string as output. My questions are: What is this…
uzay95
  • 16,052
  • 31
  • 116
  • 182
1
2 3
99 100