hashcode

Java: get a unique property of an object (like hashcode, but collision proof)

I have a task for which it is necessary to generate a unique value for every object in a set. using the hashcode would be perfect, if collisions weren't allowed in the hashcode contract. One idea: Record every object's hashcode into a multiset. Then, use hashcodes as the unique identifier, but if that hashcode is in the set more than o...

How should I implement this HashMap's equals and hashCode methods to represent an automaton state?

I want to put State objects (which are HashMaps with Character as key and State as Value into an ArrayList named allStates. Should I override the equals and hashCode methods here? Why? How? This code is for the Automaton and State classes I've built so far: class State extends HashMap<Character, State>{ boolean isFinal; boolean isIni...

Order of items in a HashMap differ when the same program is run in JVM5 vs JVM6?

Hi, I have an application which displays a collection of objects in rows, one object = one row. The objects are stored in a HashMap. The order of the rows does not affect the functionality of the application (that is why a HashMap was used instead of a sortable collection). However I have noticed that the same aplication runs different...

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("Monday"); ToDos t2 = new ToDos("Monday"); ToDos t3 = new ToDos("Tuesday"); m.put(t1, "doLaundry"); m.put(t2, "payBills"); m.put(t3, "cleanAt...

To equals and hashcode or not on entity classes, that is the question.

I have been trying to reason about the best way to handle whether it is generally good practice to implement hashcode and equals on entities (I mean entity in the general sense but in most cases it will be a JPA entity). In Chapter 24 of the Hibernate manual http://docs.jboss.org/hibernate/core/3.3/reference/en/html/best-practices.html ...

How to distinguish MethodBase in generics

I have a cache based on Dictionary<MethodBase, string> The key is rendered from MethodBase.GetCurrentMethod. Everything worked fine until methods were explicitly declared. But one day it is appeared that: Method1<T>(string value) Makes same entry in Dictionary when T gets absolutely different types. So my question is about better...

A quick string checksum function in Perl generating values in the 0..2^32-1 range

I'm looking for a Perl string checksum function with the following properties: Input: Unicode string of undefined length ($string) Output: Unsigned integer ($hash), for which 0 <= $hash <= 2^32-1 holds (0 to 4294967295, matching the size of a 4-byte MySQL unsigned int) Pseudo-code: sub checksum { my $string = shift; my $hash...

hashCode of Double.POSITIVE_INFINITY

I've recently encountered an odd situation when computing the hash Code of tuples of doubles in java. Suppose that you have the two tuples (1.0,1.0) and (Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY). Using the idiom stated in Joshua Bloch's Effective Java(Item 7), these two tuples would not be considered equal (Imagine that these t...

Hashcode and equals

For Every Equal Object their Hashcode must be equal. Java returns a unique hashcode if we do not override the hashCode() method /* A program to check hashcode values for object @Author Myth17 */ class HashValue { int x; public boolean equals(Object oo) { //if(oo instanceof Hashvalue) uncommenting ths gives error.dunno why? :| Ha...

What's the Perl equivalent of Ruby "foo".hash ?

The hash method on a Ruby String returns a number based on the string's length and content: >> "foo".hash => 876516207 What's the equivalent in Perl? ...

Scala semantics of equals/hashCode for case classes with traits

I am a newcomer to Scala. In 2.7.7, the following code abstract class C case class CC() extends C trait T val c1 = CC() val c2 = new CC() with T println(c1.hashCode == c2.hashCode,c1 equals c2) prints (false,true) whereas I would have expected (false,false) What am I missing? Thanks in advance. ...

hash of java hashtable

The hashCode of a java Hashtable element is always unique? If not, how can I guarantee that one search will give me the right element? ...

Isn't 'int GetHashCode' a bit short-sighted?

Given that .Net has the ability to detect bitness via IntPtr (looking through reflector a good amount of it is marked unsafe, though - shame) I've been thinking that GetHashCode returning an int is potentially short-sighted. I know that ultimately with a good hashing algorithm the billions of permutations offered by Int32 are absolutely...

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 getter ... For instance, Netbeans generated equals() & hashCode() will not consider the super fields ... and new HomoSapiens("M", "80", "1...

Java overriding hashCode() gets StackOverflowError

Hi, so I'm not well versed in overriding hashCode and I seem to have some infinite recursion somehow going on with the hashCode method. Here is my scenario, I have a class DuplicateCache that is a cache object that checks for duplicate objects in our system. I have a static inner class Duplicate which represents the Duplicate objects. ...

How to implement hashCode and equals method

How should I implement hashCode() and equals() for the following class in Java? class Emp { int empid ; // unique across all the departments String name; String dept_name ; String code ; // unique for the department } ...

what is an objects hashcode

If the hashCode() method is not overridden, what will be the result of invoking hashCode() on any object in Java? ...

Why did Sun specify String.hashCode() implementation?

There seems to be an ongoing debate about whether it is safe to rely on the current implementation of String.hashCode() because, technically speaking, it is guaranteed by the specification (Javadoc). Why did Sun specify String.hashCode()'s implementation in the specification? Why would developers ever need to rely upon a specific imple...

Why do I need to override the equals and hashcode method in java?

Recently I read through this Developer Works Document. The document is all about defining hashCode() and equals() effectively and correctly, but I am not able to figure out why we need to override these two methods. How can I take the decision to implement these method efficiently? ...

HashCode, trying to get the last couple letters of a word or number

I have this hashCode function that I have been trying to implement with Eclipse but my Tests keep failing. I am trying to write a Hashcode function that returns that last 2 or 3 chars of a word or number. Here is my current code: public int hashCode(String value){ String test = value; int hash = 1; int len = test.length(); System.o...