hashcode

How to find hashcode validity ?

In our application we generate hashcode from a java object and store it in the database at some level. Now my questions is if somebody generates a number by hand, is there a way i can find out whether it is a valid hashcode created by the JVM from an Object and not manually created by hand. ...

Testing string equality using hashCode()

Is there any reason why a Java string cannot be tested for equality using it's hashCode method? So basically, instead of.... "hello".equals("hello") You could use... "hello".hashCode() == "hello".hashCode() This would be useful because once a string has calculated it's hashcode then comparing a string would be as efficient as comp...

HttpRequest.GetHashCode() implementation - how often do collisions occur?

I'm trying to find a reliable to way to uniquely identify and track distinct HttpRequests in an ASP.NET web site. Does anybody know anything about the implementation of HttpRequest.GetHashCode()? Specifically, how often do collisions occur? I understand that HashCodes are not guaranteed to be unique. What I'm trying to understand is s...

Java - TreeSet and hashCode()

I have a quick question about TreeSets and hashCodes. I have a TreeSet and I'm adding objects to it, before I add an object, I check to see if it exists in the TreeSet using the contains method. I have 2 distinct objects, each of which produce a distinct hashCode using my implementation of the hashCode method, example below: public int...

Overriding equals and hashCode methods for a JavaBeans implemented in Scala

Hello, I'm working on a project using iBatis and a Java caching tool ehcache but I'm implementing the model classes in Scala. I'm having a stong feeling that I'll have to override the equals and hashCode methods to enable the cache easily manage objects on it. Since most of the required properties in the scala classes are vars, I need ...

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 default String hashCode() method). A lot of this usage could result in significant application issues if a map put displaced a HashMap entry t...

Java, Object.hashCode() result constant across all JVMs/Systems?

Is the output of Object.hashCode() required to be the same on all JVM implementations for the same Object? For example if "test".hashCode() returns 1 on 1.4, could it potentially return 2 running on 1.6. Or what if the operating systems were different, or there was a different processor architecture between instances? ...

GetHashCode issue

Hi, Can anyone help explain the following. I am having an issue with a Dictionary where ContainsKey evaluates to false while both Equals and GetHashCode for the objects are successful. Below is the output from the immediate window in Visual Studio: ?LocationToRackingGroup.Keys.ToArray()[23].Equals(location) true ?LocationToRackingGroup...

.Net Hash Codes no longer persistent?

I have an API where various types have custom hash codes. These hash codes are based on getting the hash of a string representation of the object in question. Various salting techniques are used so that as far as possible Hash Codes do not collide and that Objects of different types with equivalent string representations have differen...

ConcurrentHashMap constructor parameters?

I am wondering about the parameters for constructing a ConcurrentHashMap: initialCapacity is 16 by default (understood). loadFactor is 0.75 by default. concurrencyLevel is 16 by default. My questions are: What criteria should be used to adjust loadFactor up or down? How do we establish the number of concurrently updating threads? W...

Java: How to be sure to store unique arrays based on its values on a List

I have a number of one dimension arrays of Object[] (these objects are primitive types if it helps) I want to store these arrays in a List, but only the arrays whose contents are unique from the rest. My first aproximation was to iterate througth the arrays storing in a Set the value of Arrays.hashCode(array) and only storing the array...

Google App Engine, JDO, and equals/hashCode

I've got an app in Google App Engine that was working fine. I realized that one on of my JDO-enhanced objects that I forgot to implement equals and hashCode (I need to use the object in a set). So I did. I didn't really do anything special in these implementations, in fact I just used Eclipse to generate them. Like so: @PrimaryKey @Pers...

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 something that generates a code quickly as possible. Reading around SO and the web there seem to be a few main candidates: XORing XORing wi...

Hash function that maps similar inputs to similar outputs?

Is there a hash function where small changes in the input result in small changes in the output? For example, something like: hash("Foo") => 9e107d9d372bb6826bd81d3542a419d6 hash("Foo!") => 9e107d9d372bb6826bd81d3542a419d7 <- note small difference ...

How does Object.GetHashCode work when the GC moves an object?

If I understand correctly, in .NET the default implementation of Object.GetHashCode() returns a value based on an object's memory address (at least for reference-types). However, the garbage collector is free to move objects around in memory. Presumably the hash code doesn't change just because the GC moves an object, so is there special...

Java: implementation of notification provider vs. hashCode-driven Map

I have implemented abstract generic provider for notification bunch of generic listeners E, descendants have to override notifyListener(E) with specific notification code. For backing list of listeners I choose WeakHashMap<K,V>. Listeners must be held as weak references: abstract public class NotificationProvider<E> { private Map<E...

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 special toString() format that I want them to use. So, I thought it would be convenient to put hashCode(), equals(), and toString() into ...

Internal implementation of java.util.HashMap and HashSet

I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet. Following are the doubts popping in my mind for a while: Whats is the importance of the @Override public int hashcode() in a HashMap/HashSet? Where is this hash code used internally? I have generally seen the key of the HashMap be ...

Do all Hash-based datastructures in java use the 'bucket' concept?

The hash structures I am aware of - HashTable, HashSet & HashMap. Do they all use the bucket structure - ie when two hashcodes are similar exactly the same one element does not overwrite the other, instead they are placed in the same bucket associated with that hashcode? ...

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() { final int prime = 31; int result = prime + i; result = prime * result + j; return result; } } (If ...