Re: Real hash functions in Java
- From: "Matt Humphrey" <matth@xxxxxxxx>
- Date: Sun, 27 Apr 2008 17:24:53 -0400
"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
.
- Follow-Ups:
- Re: Real hash functions in Java
- From: Roedy Green
- Re: Real hash functions in Java
- References:
- Real hash functions in Java
- From: Royan
- Real hash functions in Java
- Prev by Date: HashMap problem: insert with hash code retrieve by index
- Next by Date: Re: HashMap problem: insert with hash code retrieve by index
- Previous by thread: Real hash functions in Java
- Next by thread: Re: Real hash functions in Java
- Index(es):
Relevant Pages
|
Loading