0

I am trying the below piece of code.

class dog{
    private String name;
    public dog(String n){
        name = n;
    }
    public String getname(){ return name; }
    public void setname(String n){ name =n;}
    public boolean equals(Object o){
        //if (( o instanceof dog )&& (((dog)o).name == name)) return true;
        if (( o instanceof dog )&& (((dog)o).name.equals(name))) return true;
        else return false;
    }

    public int  hashcode(){
        return name.length();
    }
    public String toString(){
        return "Name:"+name;
    }
} 

This is my Dog class . Now in Main method , I am trying to do the following

Map<Object,Object> m = new HashMap <Object, Object>();

dog p = new dog("GM");
dog q = new dog ("GM");
System.out.println(p.equals(q));
m.put ( new dog("GM"),"K2"); 

System.out.println(m.get(new dog("GM")));

I am getting a true and a null value. i Was expecting a K2 instead of null . Can somebody help me with this . I have overridden hashcode and equals methods . What is the thing i am missing ??

EDIT : - Changed equals function. Same results .

4
  • The String value comparison is incorrect in dog class in equal method. Commented Oct 29, 2013 at 3:09
  • You used (((dog)o).name == name)). Actually, it should use (((dog) o).name.equals(name). Please change it first and then check something further. Commented Oct 29, 2013 at 3:10
  • Change your hashCode to return name.hashCode() instead of name.length(), you are using one of the worsts hash functions you can. It won't return negatives, and it rarely uses large numbers. Commented Oct 29, 2013 at 4:13
  • And it is preferred to apply Java Coding Conventions: 1- Capitalize class name to Dog not dog 2- rename the getter and setter methods to getName and setName so you (and most of libraries) can access them using interpspection. Commented Oct 29, 2013 at 4:15

1 Answer 1

3

The immediate problem is that hashCode needs a capital C, you are implementing hashcode with a lowercase c.

((dog)o).name == name compares the identities of the strings. That means if you have two instances of the string "GM", they will .equals() each other, but not ==.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the input. But p.equals(q) is returning true. That means equals method is working as expected. Isn't ? Also in case of strings i think this is fine. Please correct me if am wrong. In fact tried that too. same results even if i use equals instead of ==
You are wrong. This is not fine in case of String; in fact it's in the case of strings where it most often confuses people. Sometimes it works (look up "string interning"), and sometimes it doesn't. Apparently for you it's working in the println but if it were working in the map we wouldn't be having this discussion.
Got it, that took way too long
You meant you got the mistake i made ? If yes please do help .
Ah Thanks a lot.. Stupid mistake :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.