-3

I have this statement about java equals and hashcode.

if we were to use such an Integer object for a key in a HashMap, we would not be able to reliably retrieve the associated value

what it means ? why it says 'not reliably' ? I write a test program, it always works.

public class Test1 {

    public static void main(String[] args){
        Map<Integer, Student> map = new HashMap<Integer, Student>();

        map.put(1, new Student("john"));        
        map.put(2, new Student("peter"));

        Student s1 = map.get(1);
        Student s2 = map.get(1);
        Student s3 = map.get(2);
        System.out.println("s1:"+s1+" s2:"+s2+" s3:"+s3);

        System.out.println(s1==s2);
        System.out.println(s1==s3);
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s2));
    }   
}

class Student{
    private String name;
    public Student(String name){        
        this.name = name;       
    }

    public String getName(){
        return this.name;
    }   
}
6
  • 6
    you need to quote more
    – hvgotcodes
    Jan 13, 2012 at 15:17
  • such an Integer object? which one???
    – Mr.Eddart
    Jan 13, 2012 at 15:18
  • 1
    Where does this statement come from? What's the full context?
    – weekens
    Jan 13, 2012 at 15:18
  • See my answer. It's a selective quote and incorrect on its own!
    – Joe
    Jan 13, 2012 at 15:20
  • -1 it's a poor question because you ignore over half the relevant paragraph and don't provide a link. Jan 13, 2012 at 15:40

3 Answers 3

12

You have misunderstood! Read the whole paragraph:

What would happen if Integer did not override equals() and hashCode()? Nothing, if we never used an Integer as a key in a HashMap or other hash-based collection. However, if we were to use such an Integer object for a key in a HashMap, we would not be able to reliably retrieve the associated value, unless we used the exact same Integer instance in the get() call as we did in the put() call.

http://www.ibm.com/developerworks/java/library/j-jtp05273/index.html

This is saying that the Integer class must have a hashCode() method and equals() in order to work. It does have those methods, so it's fine. It will work.

The example was saying that references to objects that store values should not be compared because two Integers with the same integer value may actually be different objects.

Integer x = new Integer(5);
Integer y = new Integer(5);

x.equals(y) is always true. x == y is not always true (but can be sometimes). The rules change for different ranges of value. Never rely on == for any object references unless you are completely sure you know what you are doing.

Also note that you are passing int arguments (primitive types) not Integers (references to objects) but your collection's generic type is Integer. This means there is also boxing to throw into the equation!

1
  • It's an innocent enough question.
    – Joe
    Jan 13, 2012 at 15:24
2

The full context of the quote:

What would happen if Integer did not override equals() and hashCode()? Nothing, if we never used an Integer as a key in a HashMap or other hash-based collection. However, if we were to use such an Integer object for a key in a HashMap, we would not be able to reliably retrieve the associated value, unless we used the exact same Integer instance in the get() call as we did in the put() call.

The default equality test in Java is a strict instance equality test (i.e. var1 and var2 are equal if and only if var1 and var2 point to the same object in memory). However, if you use an Integer as a key into a Map, you probably want the actual number that it represents to be the key (not the Integer instance itself), which is incompatible with this strict equality test. So, Integer must override hashCode and equals to make this work (which it does).

1
  • As a note: Integer does override hashCode and equals correctly.
    – jjmontes
    Jan 13, 2012 at 15:22
1

Your question is missing some information.

What you are talking about is the specific .hashcode() and .equals() contract. They need to follow certain properties for them to be correct, and thus usable by other classes that rely on them (Java Collections make extensive usage of .hashcode() and .equals()).

It's well explained here: http://www.technofundo.com/tech/java/equalhash.html.

The book "Effective Java" also has very good hints about these methods.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.