Results 1 to 9 of 9
  1. #1
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default Hashcode definition question!!

    Hi can anyone help me to solve this problem!!??

    Question asks what is the appropriate definition of the hashCode method in class Person???

    And answer is
    return name.hashCode() + age *7;

    I just really don't understand what is going on there!!
    Please explain me

    Thank you

    class Person {

    private String name,comment;
    private int age;
    public Person(String n,int a , String c){

    name=n; age=a;comment=c;

    }

    public boolean equals(Object o){

    if(!(o instanceof Person)) return false;
    Person p=(Person)o;
    return age==p.age && name.equals(p.name);

    }

    }

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    hashCode is a number which is unique for all objects.
    Java has its own mechanism to generate hashCode.
    You can override this hashCode() in you class also, so that the objects of your class will be assigned unique integer.

    Your question is to override the hashCode() in Person class.
    Name and age is the only two parameters which will distinguish between two objects. So inside your hashCode() method, write any code which will generate a unique number depending on the name and age.

    Remember, at times hashCode can be same for two different objects.

  3. #3
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    Hi thanks for your reply..

    But there is yet another "comment" feild ,why don't we use that?? and is 7 which is in the answer must? i think we can use any number instead of seven...

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    [QUOTE=chathura87;183668]
    But there is yet another "comment" feild ,why don't we use that??
    I'm sorry, I didn't get what you asked.

    Quote Originally Posted by chathura87 View Post
    and is 7 which is in the answer must? i think we can use any number instead of seven...
    Definitely not. You can write your own logic to calculate that number. Remember, one thing... the more uniqueness in the hashCode generated, the better your logic is.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Quote Originally Posted by subith86 View Post
    hashCode is a number which is unique for all objects.
    ...
    Remember, at times hashCode can be same for two different objects.
    The latter sentence is correct.
    The former isn't.

  6. #6
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    [QUOTE=subith86;183672][QUOTE=chathura87;183668]
    But there is yet another "comment" feild ,why don't we use that??
    I'm sorry, I didn't get what you asked.

    This is what i meant..
    These are the instance variables for person ...(age, name, and comment)

    private String name,comment;
    private int age;

    Why are we only taking age, name?? why don't we use comment??

    Thank you

  7. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Quote Originally Posted by chathura87 View Post
    Why are we only taking age, name?? why don't we use comment??
    You can. Nobody is stopping you.

    @Tolls : Thanks for the correction. Infact I didn't mean it will be unique. Thats why I ended the explanation with the correct sentence. The proper wording should have been "supposed to be unique"

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by chathura87 View Post
    Why are we only taking age, name?? why don't we use comment

    because when you compare two objects in java the question is: when are two objects equals? the answer depends on the kind of objects you want compare. for the person class the answer is when the name and the age are equals. the comment is not a real property of the person, so it's ok if it's not taken for comparing objects. but it wouldn't hurt if also the comment is taken. but watch out: when two objects are equal according to the method equals() also the hashCode() must produce the same integer. to burn in, here is the contract for overwriting the hashCode:

    1. Multiple calls to x.hashcode() must return the same integer.
    2. If x.equals(y) is true, x.hashCode() == y.hashCode must be true.
    3. If x.equals(y) is false, then x.hashCode == y.hashCode can be either true or false.

    it looks complex but overwriting the hashCode with a simple return 42; will fulfill the contract, even if this is the worst possible hash function. an efficient hashCode function becomes crucial when using collections like HashMap, HashSet or Hashtable. by the way, one recipe for computing the hashcode for a long is: (int) (f ^ (f >>>32).
    Last edited by j2me64; 03-03-2011 at 01:24 PM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by subith86 View Post
    hashCode is a number which is unique for all objects.
    That is not true; as a counter example if the hasCode() for the String "foo" is x then the Integer object with value x has the same hasCode() as the String; i.e.

    Java Code:
    new Integer(x).hashCode() == "foo".hashCode() == x
    Hash codes don't have to be unique as long as they are the same for identical objects of the same class. None identical objects of the same class can have, but need not have, different hash codes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. equals() and HashCode()
    By jomypgeorge in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 07:34 AM
  2. hashCode() contract
    By jomypgeorge in forum New To Java
    Replies: 3
    Last Post: 01-14-2011, 11:32 AM
  3. Field definition for extending classes Question.
    By MadJack in forum New To Java
    Replies: 11
    Last Post: 11-12-2010, 06:52 PM
  4. hashCode questions
    By alpdog14 in forum New To Java
    Replies: 3
    Last Post: 02-19-2010, 11:00 PM
  5. An acceptable hashcode?
    By dsym@comcast.net in forum New To Java
    Replies: 7
    Last Post: 03-29-2009, 09:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •