Questions tagged [hashcode]

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

hashcode
Filter by
Sorted by
Tagged with
1676 votes
22 answers
297k views

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 ...
bitbonk's user avatar
  • 49.2k
1660 votes
16 answers
471k 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 ...
David Basarab's user avatar
1095 votes
34 answers
1.2m views

How can I generate an MD5 hash in Java?

Is there any method to generate MD5 hash of a string in Java?
Akshay's user avatar
  • 11.9k
995 votes
77 answers
909k views

How can I 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 ...
user avatar
616 votes
11 answers
609k views

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's user avatar
535 votes
31 answers
646k 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, however I am not able to figure out why we need to ...
Batakj's user avatar
  • 12.6k
327 votes
20 answers
281k views

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's user avatar
  • 27.9k
285 votes
11 answers
285k views

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, ...
ivan_ivanovich_ivanoff's user avatar
270 votes
8 answers
142k views

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__() ...
user229898's user avatar
  • 3,227
266 votes
15 answers
257k 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 objects ...
akshay's user avatar
  • 5,275
207 votes
9 answers
82k 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 ...
Ian Dallas's user avatar
  • 12.6k
192 votes
14 answers
483k views

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, ...
Leif Andersen's user avatar
192 votes
8 answers
199k views

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's user avatar
  • 3,293
176 votes
20 answers
289k views

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 ...
Boog's user avatar
  • 3,784
174 votes
9 answers
224k views

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" ...
uzay95's user avatar
  • 16.3k
160 votes
8 answers
84k views

Apache Commons equals/hashCode builder [closed]

I'm curious to know, what people here think about using org.apache.commons.lang.builder EqualsBuilder/HashCodeBuilder for implementing the equals/hashCode? Would it be a better practice than writing ...
aug70co's user avatar
  • 3,957
152 votes
5 answers
143k views

What is hashCode used for? Is it unique?

I notice there is a getHashCode() method in every control, item, in WP7, which returns a sequence of numbers. Can I use this hashcode to uniquely identify an item? For example, I want to identify a ...
Nghia Nguyen's user avatar
  • 2,635
146 votes
8 answers
125k views

Consistency of hashCode() on a Java string

The hashCode value of a Java String is computed as (String.hashCode()): s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] Are there any circumstances (say JVM version, vendor, etc.) under which the ...
knorv's user avatar
  • 49.6k
142 votes
14 answers
237k views

How to create a HashMap with two keys (Key-Pair, Value)?

I have a 2D array of Integers. I want them to be put into a HashMap. But I want to access the elements from the HashMap based on Array Index. Something like: For A[2][5], map.get(2,5) which returns a ...
Crocode's user avatar
  • 3,116
141 votes
11 answers
158k views

.NET unique object identifier

Is there a way of getting a unique identifier of an instance? GetHashCode() is the same for the two references pointing to the same instance. However, two different instances can (quite easily) get ...
Martin Konicek's user avatar
139 votes
9 answers
59k views

GetHashCode Guidelines in C#

I read in the Essential C# 3.0 and .NET 3.5 book that: GetHashCode()’s returns over the life of a particular object should be constant (the same value), even if the object’s data changes. In many ...
Joan Venge's user avatar
  • 324k
132 votes
6 answers
152k views

How do you get the "object reference" of an object in java when toString() and hashCode() have been overridden?

I would like to print the "object reference" of an object in Java for debugging purposes. I.e. to make sure that the object is the same (or different) depending on the situation. The problem is that ...
Nicolai's user avatar
  • 3,718
131 votes
2 answers
18k views

Boolean.hashCode()

The hashCode() method of class Boolean is implemented like this: public int hashCode() { return value ? 1231 : 1237; } Why does it use 1231 and 1237? Why not something else?
user471011's user avatar
  • 7,232
129 votes
6 answers
87k views

How default .equals and .hashCode will work for my classes?

Say I have my own class public class MyObj { /* ... */ } It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode. Once we call equals and hashCode, what are ...
alexeypro's user avatar
  • 3,673
112 votes
10 answers
67k views

Quick and Simple Hash Code Combinations

Can people recommend quick and simple ways to combine the hash codes of two objects. I am not too worried about collisions since I have a Hash Table which will handle that efficiently I just want ...
RobV's user avatar
  • 28.4k
112 votes
9 answers
69k views

How should equals and hashcode be implemented when using JPA and Hibernate

How should model class's equals and hashcode be implemented in Hibernate? What are the common pitfalls? Is the default implementation good enough for most cases? Is there any sense to use business ...
egaga's user avatar
  • 21.4k
107 votes
6 answers
195k views

Hashing with SHA1 Algorithm in C#

I want to hash given byte[] array with using SHA1 Algorithm with the use of SHA1Managed. The byte[] hash will come from unit test. Expected hash is 0d71ee4472658cd5874c5578410a9d8611fc9aef (case ...
Merve Kaya's user avatar
  • 1,229
104 votes
6 answers
43k views

What's a good recipe for overriding hashcode in Dart?

I find myself wanting to override hashcode and == for an object, and I'm wondering if there are best practices for how to implement a hashcode that depends on multiple attributes, and it seems like ...
Alan Knight's user avatar
  • 2,881
89 votes
12 answers
21k views

What is an object's hash code if hashCode() is not overridden?

If the hashCode() method is not overridden, what will be the result of invoking hashCode() on any object in Java?
java_geek's user avatar
  • 17.8k
83 votes
7 answers
297k views

HashMaps and Null values?

How do you pass in null values into a HashMap? The following code snippet works with options filled in: HashMap<String, String> options = new HashMap<String, String>(); options.put("...
Neutron_boy's user avatar
80 votes
4 answers
82k views

Objects.hash() vs Objects.hashCode(), clarification needed

Since Java 7, we have o.hashCode(); Objects.hashCode(o); Objects.hash(o); The first two are roughly the same with the null check, but what is last one? When a single object reference is supplied, ...
James Raitsev's user avatar
78 votes
9 answers
13k views

Why doesn't String's hashCode() cache 0?

I noticed in the Java 6 source code for String that hashCode only caches values other than 0. The difference in performance is exhibited by the following snippet: public class Main{ static void ...
polygenelubricants's user avatar
77 votes
10 answers
32k 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, ...
donnyton's user avatar
  • 5,954
75 votes
10 answers
72k views

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 ...
wj.'s user avatar
  • 2,001
75 votes
5 answers
15k views

How does the JVM ensure that System.identityHashCode() will never change?

Typically the default implementation of Object.hashCode() is some function of the allocated address of the object in memory (though this is not mandated by the Java Language Specification). Given that ...
butterchicken's user avatar
73 votes
3 answers
76k views

What is the default implementation of `hashCode`? [duplicate]

If one does not override the hashCode method, what is the default implementation of hashCode ?
John Threepwood's user avatar
72 votes
6 answers
46k views

Should I write equals() and hashCode() methods in JPA entities?

I want to check if entity is in a Collection member (@OneToMany or @ManyToMany) of another entity: if (entity2.getEntities1().contains(entity1)) { }
j2j's user avatar
  • 821
72 votes
5 answers
83k views

Bad idea to use String key in HashMap?

I understand that the String class' hashCode() method is not guarantied to generate unique hash codes for distinct String-s. I see a lot of usage of putting String keys into HashMap-s (using the ...
Marcus Leon's user avatar
  • 55.8k
71 votes
6 answers
23k views

What is a sensible prime for hashcode calculation?

Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would generate for example (slightly shortened:) class HashTest { int i; int j; public int hashCode() ...
Dr. Hans-Peter Störr's user avatar
66 votes
11 answers
102k views

How is hashCode() calculated in Java

What value does the hashCode() method return in java? I read that it is a memory reference of an object... The hash value for new Integer(1) is 1; the hash value for String("a") is 97. I am ...
Jothi's user avatar
  • 15k
64 votes
12 answers
61k views

toString(), equals(), and hashCode() in an interface

So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant. The objects that implement this interface are often put into collections, and also have a ...
J_Y_C's user avatar
  • 697
63 votes
9 answers
119k views

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("...
andandandand's user avatar
  • 22.2k
63 votes
9 answers
53k views

GetHashCode override of object containing generic array

I have a class that contains the following two properties: public int Id { get; private set; } public T[] Values { get; private set; } I have made it IEquatable<T> and overriden the ...
Svish's user avatar
  • 155k
62 votes
4 answers
36k views

hashCode in case classes in Scala

I've read that Scala'a case class construct automatically generates a fitting equals and hashCode implementation. What does exactly the generated code look like?
user avatar
62 votes
7 answers
76k views

How does Java order items in a HashMap or a HashTable?

I was wondering how Java orders items in the Map (HashMap or Hashtable) when they are added. Are the keys ordered by the hashcode, memory reference or by allocation precedence...? It's because I've ...
Eyad Salah's user avatar
  • 1,084
62 votes
3 answers
21k views

How do I create a HashCode in .net (c#) for a string that is safe to store in a database?

To quote from Guidelines and rules for GetHashCode by Eric Lippert: Rule: Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains Suppose you have a Customer ...
Ian Ringrose's user avatar
  • 51.5k
62 votes
8 answers
23k views

Is it possible in java make something like Comparator but for implementing custom equals() and hashCode()

I have an array of objects and I want to concatenate it with another array of objects, except that objects that have same id's. That objects are used in many places in the system and don't have hash ...
dhblah's user avatar
  • 9,911
60 votes
4 answers
62k views

Java Array HashCode implementation

This is odd. A co-worker asked about the implementation of myArray.hashCode() in java. I thought I knew but then I ran a few tests. Check the code below. The odd thing I noticed is that when I ...
souLTower's user avatar
  • 686
57 votes
4 answers
15k views

Why are XOR often used in java hashCode() but another bitwise operators are used rarely?

I often see code like int hashCode(){ return a^b; } Why XOR?
Andrei N's user avatar
  • 4,756
57 votes
7 answers
30k views

How do hashCode() and identityHashCode() work at the back end?

How do Object.hashCode() and System.identityHashCode() work at the back end? Does identityHashCode() return the reference of the object? Does hashCode() depend on the content or address of the object? ...
Himanshu's user avatar
  • 901

1
2 3 4 5
41