What is the best way to get the Hash value for a String

Posted on 2004-09-03
Last Modified: 2010-05-18
Here is the Sun's way

    public int hashCode() {
      int h = hash;
      if (h == 0) {
          int off = offset;
          char val[] = value;
          int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            hash = h;
        return h;
Question by:sudhakar_koundinya
  • 4
  • 3
LVL 24

Accepted Solution

sciuriware earned 125 total points
ID: 11979178
In general a hashing algorithm should spread a collection to a flat model.
In other words, it depends. If your initial collection has a majority of similar strings it will be
hard to create any algorithm. The SUN approach will work most of the time,
but in many cases it's easier to look at your data.
For instance you might consider a ZIP code as part of the hash value.
LVL 86

Expert Comment

ID: 11979710
Hash algorithms are a trade-off between speed and the ability to prevent code collisions. Why are you questioning Sun's implementation btw?
LVL 86

Expert Comment

ID: 11979726
Sorry - but i don't really understand that accepted answer - perhaps someone can explain it to me? ;-)
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 24

Expert Comment

ID: 11980880
CEHJ, I agree that a split was as acceptable as an A would have been.
Didn't you see in many cases that the award goes to the expert who (accidentally)
   hits a preception? Now you can determine what the question would have been.
Sorry, next question please.
LVL 86

Expert Comment

ID: 11981928
>>CEHJ, I agree that a split was as acceptable ...

I think you misunderstood - i was actually saying i didn't understand your answer, not 'i don't understand why that answer has been accepted' (not that i'm ruling out a connection between the two ;-))
LVL 24

Expert Comment

ID: 11983484
Sorry, well, the idea is that if some record seems to contain an unique piece of data, that piece is eligible
for being used as hash (or any other) key.
When the spread of such a key over the total collection is about flat (no accumulation in some spots)
it is ideal for hashing (few collisions).
So, before you apply a general algorithm, look an the nature of your data.
For instance a phone number might be an unique part of a record, but if most of those start with the same digits
you will take some substring from it that doesn't always start the same.
That was my message.

LVL 86

Expert Comment

ID: 11983649
I'm not sure it's an answer to the question asked, but .. OK ;-)

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 84
Exception after setting jdbc session management 2 38
wordcount challenge 11 76
Image decoding from Camera 3 45
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now