hashCode()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    hashCode()

    Hi,
    I have wrapped to strings in a object using hashCode of these strings.......N ow I want to know how can I get back these strings from the object......... ......I am putting this object as a key in a HashMap.....For more details kindly look here
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by madhoriya22
    Hi,
    I have wrapped to strings in a object using hashCode of these strings.......N ow I want to know how can I get back these strings from the object......... ......I am putting this object as a key in a HashMap.....For more details kindly this <a href = 'http://www.thescripts. com/forum/thread675010.ht ml'>post</a>
    Why (and how) did you use hashCode to make objects out of the Strings?
    Maybe if you post your code ...

    Comment

    • madhoriya22
      Contributor
      • Jul 2007
      • 251

      #3
      Originally posted by r035198x
      Why (and how) did you use hashCode to make objects out of the Strings?
      Maybe if you post your code ...

      If you will look at that hyperlink...... .then you will get what I am trying to say here......by d way code is like:-
      Code:
      class WorkPackageMapKey {
        private String name;
        private String status;
        // constructors, getters, and setters as usual
        // for the above properties.
      
        // the following methods override those
        // in Object and are recommended to make a
        // logically correct "key" behavior. the exact
        // details are up to you, but equals and hashcode
        // must be consistent.
      
        public boolean equals(Object o) {
          if (this == o) return true;
          if ( ! (o instanceof WorkPackageMapKey)) {
            return false;
          }
          WorkPackageMapKey wpmk = (WorkPackageMapKey)o;
          if ( ! this.name.equals(wpmk.name)) {
            return false;
          }
          if ( ! this.status.equals(wpmk.status)) {
            return false;
          }
          //etc
          return true;
        }
      
        // there's a PDF Sun made available to correctly account
        // for primitive types, but if you don't have those...
      
        public int hashCode() {
          int hashcode = name.hashCode();
          hashcode += status.hashCode();
          //etc
          return hashcode;
        }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by madhoriya22
        If you will look at that hyperlink...... .then you will get what I am trying to say here......by d way code is like:-
        I saw it. Look at Jos's example again. Where's your constructor that takes the two strings?
        If you want to get each string individually, just provide getters for them. That has nothing to do with hashCode.

        Comment

        • madhoriya22
          Contributor
          • Jul 2007
          • 251

          #5
          Originally posted by r035198x
          I saw it. Look at Jos's example again. Where's your constructor that takes the two strings?
          If you want to get each string individually, just provide getters for them. That has nothing to do with hashCode.

          If that has nothing to do with hashCode....... ...then why we are implementing equals() and hashCode() method?......If simply I have to wrap it in a object then I have Value Object(VO) class........I can do it with that also......Plz correct me if I am wrong.......... .....

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by madhoriya22
            If that has nothing to do with hashCode....... ...then why we are implementing equals() and hashCode() method?......If simply I have to wrap it in a object then I have Value Object(VO) class........I can do it with that also......Plz correct me if I am wrong.......... .....
            I didn't say equals and hashCode are not important. You should include them always but they are not used for solving the question you asked in this thread. Read this thread again from the start.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by madhoriya22
              Code:
                public int hashCode() {
                  int hashcode = name.hashCode();
                  hashcode += status.hashCode();
                  //etc
                  return hashcode;
                }
              Not that it matters much, but a slightly better hashCode method is this:

              [code=java]
              public int hashCode() {
              return 3*name.hashCode ()+status.hashC ode();
              }
              [/code]

              It gives different hash values for (a,b) and (b,a) while your version returns
              identical values for those pairs of strings.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by JosAH
                Not that it matters much, but a slightly better hashCode method is this:

                [code=java]
                public int hashCode() {
                return 3*name.hashCode ()+status.hashC ode();
                }
                [/code]

                It gives different hash values for (a,b) and (b,a) while your version returns
                identical values for those pairs of strings.

                kind regards,

                Jos
                Something which the OP may need to think about. Do they want key (a, b) to map to the same value mapped to by key (b, a) ?

                Comment

                • madhoriya22
                  Contributor
                  • Jul 2007
                  • 251

                  #9
                  Originally posted by r035198x
                  Something which the OP may need to think about. Do they want key (a, b) to map to the same value mapped to by key (b, a) ?

                  Actually my key value mapping will be like this:-
                  Code:
                  1.  K(a1,b1) ---->  v1
                  2.  .............
                  4.  K(a1,b4) ---->  v4
                  
                  5.  K(a2,b1) ---->  v5
                  6.  .............
                  8.  K(a2,b4) ---->  v8
                  
                  9.  K(a3,b1) ---->  v9
                  10.....................so on.....
                  Values(number) can be same or different for all keys..........s o I dont think... here is any case of key(a,b) or key(b,a) mapping to same value

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by madhoriya22
                    Actually my key value mapping will be like this:-
                    Code:
                    1.  K(a1,b1) ---->  v1
                    2.  .............
                    4.  K(a1,b4) ---->  v4
                    
                    5.  K(a2,b1) ---->  v5
                    6.  .............
                    8.  K(a2,b4) ---->  v8
                    
                    9.  K(a3,b1) ---->  v9
                    10.....................so on.....
                    Values(number) can be same or different for all keys..........s o I dont think... here is any case of key(a,b) or key(b,a) mapping to same value
                    Is your map a full map, i.e. for keys a1 ... a4 and b1 ... b4 are there 16 compound
                    keys in your map?

                    kind regards,

                    Jos

                    Comment

                    • madhoriya22
                      Contributor
                      • Jul 2007
                      • 251

                      #11
                      Originally posted by JosAH
                      Is your map a full map, i.e. for keys a1 ... a4 and b1 ... b4 are there 16 compound
                      keys in your map?

                      kind regards,

                      Jos

                      Ya it is a full map........cant say that there will be exact 16 compound keys..........b ut key value combinations will be like that only(last reply)...

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by madhoriya22
                        Ya it is a full map........cant say that there will be exact 16 compound keys..........b ut key value combinations will be like that only(last reply)...
                        So for n and m keys you always have n*m values in your map? If so you have
                        a Cartesian product and you can handle that with two simple maps and a two
                        dimensional array of values.

                        kind regards,

                        Jos

                        Comment

                        Working...