Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-03
7
Medium Priority
?
144 Views
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;
    }
0
Comment
Question by:sudhakar_koundinya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 375 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.
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11979726
Sorry - but i don't really understand that accepted answer - perhaps someone can explain it to me? ;-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:sciuriware
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.
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
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 ;-))
0
 
LVL 24

Expert Comment

by:sciuriware
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.

;JOOP!
0
 
LVL 86

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 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