hashcode

.NET unique object identifier

Is there any way of getting an unique identifier of an instance? GetHashCode() is same for 2 references pointing to the same instance. However, 2 different instances can (quite early) get same hash code: Hashtable hashCodesSeen = new Hashtable(); LinkedList<object> l = new LinkedList<object>(); int n = 0; while (true) { ob...

GetHashCode Extension Method

After reading all the questions and answers on StackOverflow concerning overriding GetHashCode() I wrote the following extension method for easy and convenient overriding of GetHashCode(): public static class ObjectExtensions { private const int _seedPrimeNumber = 691; private const int _fieldPrimeNumber = 397; public static...

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 following expression will evaluate to false? boolean expression = "This is a Java string".hashCode() == 586653468 Update #1: If you claim th...

Proof: why does java.lang.String.hashCode()'s implementation match its documentation?

The JDK documentation for java.lang.String.hashCode() famously says: The hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. The standard implementation of ...

HashCode vs SHA-1

Hi.. I'd like to compare some large objects representing trees and cache something to avoid comparing each time the new object with one already existing... The question is what would be the best something ? (a compromise between performance and collisions...). On the one hand, I have a regular hashCode function based on the value of v...

Data Structure for enumeration in c# where lookup is often predicated on one property of the Objects being stored

I'm wondering what Data Structure people would recommend to do the following. I have a Class which has three main properties eg. public class Example { public Object One { get; } public Object Two { get; } public Object Three { get; } } Another class contains a collection of these Objects and frequently needs to enumerate over...

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

Hello! 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 return distinct integers for distinct objects. But when the class overrides hash...

Should I use a concatenation of my string fields as a hash code?

I have an Address class in C# that looks like this: public class Address { public string StreetAddress { get; set; } public string RuralRoute { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }...

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 JLS). Given that the VM shunts objects about in memory, why does the value returned by System.identityHashCode() never change during the object's lifetime? If it is a "one-shot" ...

Java hashcode based on identity

The default behavior of Object.hashCode() is to return essentially the "address" of the object so that a.hashCode() == b.hashCode() if and only if a == b. How can I get this behavior in a user-defined class if a superclass already defines hashCode()? For instance: class A { public int hashCode() { return 0; } } class B extends...

Efficient hashCode() implementation

I often auto-generate an class's hashCode() method using IntelliJ IDEA and typically the method takes the form: result = 31 * result + ... My question is what is the purpose of multiplying by 31? I know this is a prime number but why pick 31 specifically? Also, if implementing a hashCode() for a particularly small / large dataset wo...

Double in HashMap

I was thinking of using a Double as the key to a HashMap but I know floating point comparisons are unsafe, that got me thinking. Is the equals method on the Double class also unsafe? If it is then that would mean the hashCode method is probably also incorrect. This would mean that using Double as the key to a HashMap would lead to unpred...

Is it possible to combine hash codes for private members to generate a new hash code?

I have an object for which I want to generate a unique hash (override GetHashCode()) but I want to avoid overflows or something unpredictable. The code should be the result of combining the hash codes of a small collection of strings. The hash codes will be part of generating a cache key, so ideally they should be unique however the nu...

Dynamic perfect hashing and universal hash functions - explanation please?

So I'm reading up about hash tables, hash functions etc. I was intrigued to read on wikipedia about how "dynamic perfect hashing" involves using a second hash table as the data structure to store multiple values within a particular bucket. Where I get lost however, is when it comes to how a universal hash function is selected to perfor...

Creating the GetHashCode method in C#

What is the best way to create your own GetHashCode method for a class in C#? Suppose I have a simple class (which overrides the Equals method), as follows: class Test { public string[] names; public double[] values; public override bool Equals(object obj) { return (obj is Test) && this.Equals((...

.NET equivalent to java.util.Arrays.hashCode() function for arrays of intrinsic types?

Is there a.NET utility class equivalent to java.util.Arrays.hashCode() for arrays of intrinsic types such as int[], short[], float[], etc.? Obviously I could write my own utility class but was trying to find one already available in the .NET framework. ...

What's the optimal way to compute a hashcode for a set of points?

I'm looking for the optimal way to compute a hashcode for a set of bi-dimensional points (so that I can store polygons in a hashtable). There are some obvious ways to do that, such as concatenating all the points coordinates in a string and its hashcode, but this would be very slow. On the other end of the speed/collision spectrum, I c...

HashCodeBuilder in C++

In java, if I want generate a id for a given object, the easyest way I know is to use apache commons: public class Person { String name; int age; boolean smoker; ... public int hashCode() { // you pick a hard-coded, randomly chosen, non-zero, odd number // ideally different for each class return new HashCo...

Does Scala's BigDecimal violate the equals/hashCode contract?

As the Ordered trait demands, the equals method on Scala's BigDecimal class is consistent with the ordering. However, the hashcode is simply taken from the wrapped java.math.BigDecimal and is therefore inconsistent with equals. object DecTest { def main(args: Array[String]) { val d1 = BigDecimal("2") val d2 = BigDecimal("2.00"...

General advice and guidelines on how to properly override object.GetHashCode()

According to MSDN, a hash function must have the following properties: If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values. The GetHashCode method for...