This discussion is archived
1 2 Previous Next 18 Replies Latest reply: Feb 17, 2010 9:03 AM by 796440 RSS

Java hash code with long strings

807580 Newbie
Currently Being Moderated
Hi
I have a set of 6 keys that are represented by a string but with long length such like 20 character each string .
I want to save these 12 keys as one key in HashMap is it recommended to concatenate all the string and put it as a key in the map ,I think it’s not good
Because there is a big overhead of performance while calculation the hash code method ,can any one please suggest how java can deal with this issue

Thanks
  • 1. Re: Java hash code with long strings
    JoachimSauer Journeyer
    Currently Being Moderated
    Eddie404 wrote:
    Hi
    I have a set of 6 keys that are represented by a string but with long length such like 20 character each string .
    6*20 characters is 120 characters, that's not long.
    I want to save these 12 keys as one key in HashMap is it recommended to concatenate all the string and put it as a key in the map
    So in a simplified example if the Strings were "foo" and "bar" then they should map to the same value as "foob" and "ar"?
    ,I think it’s not good
    Because there is a big overhead of performance while calculation the hash code method
    What makes you think that this performance relevant for your entire application?

    Are you having performance problems with your current applicaton?
    Did you do any profiling?
    Did the profiling show that calculating the hashCode of those Strings is the bottleneck?

    If you answer "no" to any of those 3 questions, then ignore the performance of that solution for the moment.
  • 2. Re: Java hash code with long strings
    807580 Newbie
    Currently Being Moderated
    Thanks for your reply

    the performance issue is so critical here therefore I am searching about a smart way to make mapping of 120 characters
  • 3. Re: Java hash code with long strings
    796262 Newbie
    Currently Being Moderated
    Eddie404 wrote:
    Thanks for your reply

    the performance issue is so critical here therefore I am searching about a smart way to make mapping of 120 characters
    What performance issue? What performance problems have you experienced that was caused by using 120 characters?
  • 4. Re: Java hash code with long strings
    JoachimSauer Journeyer
    Currently Being Moderated
    Eddie404 wrote:
    Thanks for your reply

    the performance issue is so critical here
    So you can answer "yes" to all 3 questions?
  • 5. Re: Java hash code with long strings
    807580 Newbie
    Currently Being Moderated
    Eddie404 wrote:
    Thanks for your reply

    the performance issue is so critical here therefore I am searching about a smart way to make mapping of 120 characters
    I have read this thread several times and I still cannot work out a requirement.

    I would expect that any performance problem will be associated with accessing the strings and not just inserting them into a structure. Since you don't really say how you want to access them, I doubt if anyone can help. You seem to need some form of map but I don't really understand what the keys will be or what the values associated with the keys will be.

    Edited by: sabre150 on Feb 17, 2010 3:11 PM
  • 6. Re: Java hash code with long strings
    baftos Expert
    Currently Being Moderated
    JoachimSauer wrote:
    Did the profiling show that calculating the hashCode of those Strings is the bottleneck?
    Another qustion: Are you aware that hashcodes of (immuatble!) String objects are calculated only once, when the hashcode is first needed?
        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;
        }
    Sure, this is an implementation detail, it may change, but you can count on Sun or whoever might provide a Java implementation to do it as efficient as possible.
  • 7. Re: Java hash code with long strings
    796440 Guru
    Currently Being Moderated
    Eddie404 wrote:
    is it recommended to concatenate all the string
    I wouldn't do that. Not for any imaginary "efficiency" reasons, but because, so far as I can tell, those Strings represent 6 separate pieces of information. Don't concatenate them unless the result is meaningful on its own. If you want to use the 6 Strings for the key, make them fields in a class. If you find hashing to be a performance bottleneck (by actually measuring it, not just by guessing), then you can do what somebody has already pointed out String does--cache that object's hashCode value (as long as you don't change its contents after creation--which you mustn't do for keys in hash-based data structures anyway).
  • 8. Re: Java hash code with long strings
    807580 Newbie
    Currently Being Moderated
    Am I missing something really really obvious in this thread? Everyone else seems to understand but I still don't know what the OP is trying to achieve.
  • 9. Re: Java hash code with long strings
    796440 Guru
    Currently Being Moderated
    sabre150 wrote:
    Am I missing something really really obvious in this thread? Everyone else seems to understand but I still don't know what the OP is trying to achieve.
    I'm not totally sure, but as near as I can tell, he wants to combine 6 20-char strings to make a key into a HashMap, and he's assuming that calculating hashCode() for that key will be "inefficient."
  • 10. Re: Java hash code with long strings
    807580 Newbie
    Currently Being Moderated
    jverd wrote:
    sabre150 wrote:
    Am I missing something really really obvious in this thread? Everyone else seems to understand but I still don't know what the OP is trying to achieve.
    I'm not totally sure, but as near as I can tell, he wants to combine 6 20-char strings to make a key into a HashMap, and he's assuming that calculating hashCode() for that key will be "inefficient."
    The problem I have is understanding what the HashMap contains. It's a map so it has keys and values so is it that each key is in effect an ordered list of 20 character strings? If so, how many possible 20 char sub-key strings are there?

    Until I get a better understanding of what the OP is trying to do I find it impossible to even think about optimising a solution. Since the OP seems to have left the forum I doubt if I will ever understand.
  • 11. Re: Java hash code with long strings
    807580 Newbie
    Currently Being Moderated
    the problem is that i have a 6 string and reach string in 20 length. i want to first concatenate all the string and then to insert it to a map i will do this process 1000 times and after then i need to new data and check if this data is also in the map that mean go over the map again and check 1000 elements that they are exist in the map
    so the hashcode use is high here during the inserting to the map and during the compare step,
    somene suggest not to make the concatenatation of the 6 string but added it to a class is this more efficient and how.

    Thanks
  • 12. Re: Java hash code with long strings
    DrClap Expert
    Currently Being Moderated
    1000 times? Is that all? Big deal. Even if you wrote an ordinary program -- and that's what you should do -- it wouldn't take anywhere near a second to do that. Stop wasting time with this efficiency obsession and start learning to write programs.
  • 13. Re: Java hash code with long strings
    796440 Guru
    Currently Being Moderated
    sabre150 wrote:
    jverd wrote:
    sabre150 wrote:
    Am I missing something really really obvious in this thread? Everyone else seems to understand but I still don't know what the OP is trying to achieve.
    I'm not totally sure, but as near as I can tell, he wants to combine 6 20-char strings to make a key into a HashMap, and he's assuming that calculating hashCode() for that key will be "inefficient."
    The problem I have is understanding what the HashMap contains. It's a map so it has keys and values so is it that each key is in effect an ordered list of 20 character strings? If so, how many possible 20 char sub-key strings are there?
    Oh, yeah, I'm nowhere near there yet. I have no idea what the values are or what he's trying to accomplish. I'm just trying to tell him a) don't concat 20 strings just to make a single value out of them for the key and b) don't prematurely optimize
    Until I get a better understanding of what the OP is trying to do I find it impossible to even think about optimising a solution. Since the OP seems to have left the forum I doubt if I will ever understand.
    Agreed.
  • 14. Re: Java hash code with long strings
    796440 Guru
    Currently Being Moderated
    Eddie404 wrote:
    the problem is that i have a 6 string and reach string in 20 length. i want to first concatenate all the string
    Why?

    Does that concatted string have some meaning on its own? Or are you just doing it so you can turn 6 values into one? If it's the second one, don't do that.
    and then to insert it to a map
    As the key or the value?
    i will do this process 1000 times
    Okay. So what?
    and after then i need to new data and check if this data is also in the map that mean go over the map again and check 1000 elements that they are exist in the map
    If you're checking for a key, then no, it's very fast--O(1). If you're checking for a value, then yes, you'll have to iterate, and hashCode won't be used, and you should probably have used a different data structure in the first place.
    so the hashcode use is high here during the inserting to the map and during the compare step,
    You're making incorrect assumptions about how hashCode will be used, and you're assuming it will be "too slow" without actually measuring.

    Have you even bothered to read what people have been telling you?
    somene suggest not to make the concatenatation of the 6 string but added it to a class is this more efficient and how.
    Efficiency has nothing to do with it. It's better design (unless the concatted string actually has some meaning on its own).
1 2 Previous Next