7 Replies Latest reply: Dec 12, 2008 4:11 PM by 843785 RSS

    How to use Hashmap's containsKey method for Objects

    801487
      Hi Friends,
      I have a custom class Person and I would like to store only unique Person objects into a hashmap. But I am confused on how to compare objects using the hashmap's containKey method.

      So,I am trying something like this:
      HashMap<Person,Integer> persons = new HashMap<Person,Integer>();
      Person p = new Person("tester"); //person constructor with name
      
      //add person object only if it does not exists
      if(persons.containsKey(p)){ // this is what i am confused with
       //do something
      }
      else{
      persons.add(p,1);
      }
      But I am connfused how to compare objects of a particular class??? Do I need to add "equals" or some other method to the Person class or is there any other way???

      Any help would be appreciated
      Thanks
        • 1. Re: How to use Hashmap's containsKey method for Objects
          843785
          You want to map a person to an Integer? Are you sure you don't really want to map an Integer to a person?
          • 2. Re: How to use Hashmap's containsKey method for Objects
            801487
            OK,lets go the other way around but I guess the original questions still holds, how to compare the Person objects?
            • 3. Re: How to use Hashmap's containsKey method for Objects
              843785
              Micks80 wrote:
              OK,lets go the other way around but I guess the original questions still holds, how to compare the Person objects?
              I general: any class you use as the key class for a hash table, like HashMap, should override equals and hashCode methods.
              • 4. Re: How to use Hashmap's containsKey method for Objects
                843785
                Micks80 wrote:
                But I am connfused how to compare objects of a particular class??? Do I need to add "equals" or some other method to the Person class or is there any other way???
                To use a class with a HashMap (or any hash based collection) you need to override both equals and hashCode. Equals defines whether two objects of a class are to be considered equal by the HashMap. HashCode is used by the HashMap (as part of the key function all hash based structures use) to know where to store an object internally.

                Equals and hashCode must be designed to work together according to special rules.
                • 5. Re: How to use Hashmap's containsKey method for Objects
                  801487
                  I general: any class you use as the key class for a hash table, like HashMap, should override equals and hashCode methods.
                  So you mean this is ok???
                  persons.containsKey(p);
                  • 6. Re: How to use Hashmap's containsKey method for Objects
                    843785
                    Micks80 wrote:
                    I general: any class you use as the key class for a hash table, like HashMap, should override equals and hashCode methods.
                    So you mean this is ok???
                    persons.containsKey(p);
                    It's okay if the Person class correctly overrides equals and hashCode.
                    • 7. Re: How to use Hashmap's containsKey method for Objects
                      843785
                      Micks80 wrote:
                      I general: any class you use as the key class for a hash table, like HashMap, should override equals and hashCode methods.
                      So you mean this is ok???
                      persons.containsKey(p);
                      I mean if you are using Person as the key class, you need to override its hashCode and equals methods. For example:
                      public class Person {
                          private String x, y, z;
                      
                      //...
                      
                          @Override public int hashCode() {
                              int result = 17;
                              result = 37*result + x.hashCode();
                              result = 37*result + y.hashCode();
                              result = 37*result + z.hashCode();
                              return result;
                          }
                      
                          @Override public boolean equals(Object obj) {
                              if (obj instanceof Person) {
                                  Person that = (Person) obj;
                                  return
                                      this.x.equals(that.x) &&
                                      this.y.equals(that.y) &&
                                      this.z.equals(that.z);
                              } else {
                                  return false;
                              }
                          }
                      }