Re: Real hash functions in Java




"Royan" <romayankin@xxxxxxxxx> wrote in message
news:62d071dc-52d9-4ed0-bac5-0e38e6fac9f0@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm looking for an implementation of a fair hash function in standard
java distribution (hashCode() is not of such kind)

What do you mean by "fair"? A perfect hash function is one that maps each
key to a unique value--there are algorithms to compute these but you have to
know the keys in advance. There is no general-purpose hash function that
works optimally for all data--you have to design one that takes advantage
of the details of your particular data. Java does not automatically give
you classes a meaningful hashCode--you have to provide it yourself (and make
sure it is compatible with the equals method) Other classes such as String,
subclasses of Number, etc will have meaningful hashCode functions.


My task is to hash some character data (less then 200 chars) into an
int. I thought about java.security.MessageDigest but I can't seem to
figure out how to use it in my case.

Presuming this data is in a String, is there a reason aString.hashCode() is
not satisfactory for you?

Message digests take bytes as input and return a short byte array containing
the digest. You can give it String data by specifying which encoding to
use, as in

MessageDigest md = new MessageDigest ("SHA");
md.update (aString.getBytes ("UTF-8"));
byte [] digest = md.digest ();

The digest bytes can be converted into a int via a number of ways. What
part were you having trouble with.

Then I've found
UUID#fromString(String name) and it appears to produce what i need (a
unique id from a string), but I don't know if it provides any
guaranties on originality of the produced hashes
Any help is appreciated

The fromString method does not generate the UUID for you--it simply parses
the string according to a published format (see toString) to reconstitute a
new UUID instance. The string input is not a key--it is formatted
information. The randomUUID method will create a UUID for you, but it is
random and will not have any association to the key.

Matt Humphrey http://www.iviz.com



.



Relevant Pages

  • Re: type of hash functions
    ... the other hash function. ... but rather reduce the probability that you *need* to as much ... the mere process of storing and comparing hashes not worth the effort. ... pointer to the string) first can help (since many libraries omit this ...
    (comp.programming)
  • Re: type of hash functions
    ... the other hash function. ... as long as the input string. ... exists, then for any string, the pair of keys generated (by the pair ... the original input string, even if this pair of functions does exist, ...
    (comp.programming)
  • Re: hashcode values
    ... int values) and store the hash code as the key in a Hashtable. ... Later on I recieve some data and rebuild the string w/ the ... on the hashcode which is the key. ...
    (comp.lang.java.help)
  • Re: Referenz auf Hashset
    ... String s1 = stringBuilder.toString.intern; ... Werden die 'singularisierten' Objekte vom Programm nirgendwo mehr gebraucht, kann die GC sie problemlos einsammeln, da der Singularizer nur schwache Referenzen darauf hält. ... private final int hashCode; ...
    (de.comp.lang.java)
  • Re: hashCode() for Custom classes
    ... I am currently faced with the task of providing a logical equals() ... have to override the hashCode() so that when an object of this class ... String name; ... The hash code of Agent is the combined hash code of ...
    (comp.lang.java.programmer)

Loading