hashcode

A couple of questions about Hash Tables

I've been reading a lot about Hash Tables and how to implement on in C and I think I have almost all the concepts in my head so I can start to code my own, I just have a couple of questions that I have yet to properly understand. As a reference, I've been reading this: http://eternallyconfuzzled.com/jsw_home.aspx 1) As I've read on the...

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 test(String s) { long start = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { s.hashCode(); } ...

Hashcode comparison problem

I have list of a an object which is termed as rule in our case, this object itself is a list of field for which I have to do hashcode comparison as we can't duplicate rule in the system. i.e Let say I have two Rules R1 and R2 with fields A & B. Now if values of A & B in R1 are 7 and 2 respectively. And in R2 it's 3 and 4 respectivel...

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? ...

python, hash function selection

Hello Using Python and Django, I will let my users to give pdf based gifts to their friends, which the said friend will be able to claim pdf by entering to my site from the emailed link. Here is the plan User gives a gives to his friend, enters friends email In the background, a gift model is saved which will contain a uniquely gener...

What is the best way to know if Object has been changed?

Hey! I made some "save" bean functionality in my Java Web Application (JSF1.2, RichFaces). It is using JAXB to turn it into an XML string and then it is stored in the database. If the user loads this back, I want to notify the user if the (bean)content is changed and it should be saved again. My idea is to override the hashCode() funct...

How stupid it is to use an integer value a key for an Hash Table?

Hi, I'll be needing to use Hash Tables with different keys. One as string for the key and another as integer. As for the integer one, how stupid it is to run a hash function on the number to generate a key? I mean, the numbers I'll be using as key for the Hash Table will always be different, there will be no duplicates at all. Isn't e...

How to decide the hashcode value for storing words of a dictionary?

I am preparing for my interview and came across this question: Consider that i have 1000,000 words and i want to create a dictionary . The data structure i can use is Map or B+ trees . But on what criteria should i write my hashcode(), so that the retrieval can be fast. would welcome everybody views... ...

How is hashCode() calculated in Java.

What value is hashCode() method is returning in java?. i read that it is an memory reference of an object. when i print hascode value for new Integer(1), its 1. for String(a) - 97. so i confused. is it ascii or what type of value is? ...

Complex HashMap has different hashCode after serialization

I am parsing a xml file into a complex HashMap looking like this: Map<String, Map<String, EcmObject> EcmObject: public class EcmObject implements Comparable, Serializable { private final EcmObjectType type; private final String name; private final List<EcmField> fields; private final boolean pages; // getter, equ...

Generating equals / hashcode / toString using annotation

I believe I read somewhere people generating equals / hashcode / toString methods during compile time (using APT) by identifying which fields should be part of the hash / equality test. I couldn't find anything like that on the web (I might have dreamed it ?) ... That could be done like that : public class Person { @Id @GeneratedValu...

Why does a HashMap rehash the hashcode supplied by the Key object

I am reading up the code of the HashMap class provided by the java 1.6 API and unable to fully understand the need of the following operation (found in the body of put and get methods) : int hash = hash(key.hashCode()); where the method hash() has the following body: private static int hash(int h) { h ^= (h >>> 20) ^ (h >>>...

Hash Code for a group of three fields

I have three fields namely Number1 Number2 Time I am trying to write a function in java that returns a unique hash value (long needs to be the return type of hash) for the above fields. This hash would then be used to store database rows corresponding to the above mentioned fields in a HashSet. I am new to writing a hash code functio...

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 idea, or is it a bad one? I am doing this in Java, but I wouldn't imagine that would mak...

Does hashCode() have to return a prime number?

The signature of the hashCode() method is public int hashCode(){ return x; } in this case x must be an int(primitive) but plz can anyone explain it to me that the number which the hashCode() returns must be a prime number, even number...etc or there is no specification ? the reason behind i am asking this question is i have seen ...

Consistent Equals() results, but inconsistent TreeMap.containsKey() result

I have the following object Node: private class Node implements Comparable<Node>(){ private String guid(); ... public boolean equals(Node o){ return (this == o); } public int hashCode(){ return guid.hashCode(); } public int compareTo(Node o...

Good resource for explaining how hash codes are used in collections

Hi, Can anyone give a good explanation and / or links to a good resource of how hash codes are used in storing and retrieving objects in hashtables, dictionaries etc, specifically in C# / .NET. I'm interested to see how Equals and GetHashCode are used collectively when storing and retrieving items. ...

Why should I override hashCode() when I override equals() method?

Ok, I have heard from many places and sources that whenever I override the equals() method, I need to override the hashCode() method as well. But consider the following piece of code package test; public class MyCustomObject { int intVal1; int intVal2; public MyCustomObject(int val1, int val2){ intVal1 = val1; ...

How do I calculate a good hash code for a list of strings?

Background: I have a short list of strings. The number of strings is not always the same, but are nearly always of the order of a “handful” In our database will store these strings in a 2nd normalised table These strings are never changed once they are written to the database. We wish to be able to match on these strings quickly in a...

Example of ==, equals and hashcode in java

Given this: String s1= new String("abc"); String s2= new String("abc"); String s3 ="abc"; System.out.println(s1==s3); System.out.println(s1==s2); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); Output is: ...