Value found from Map not consistent with identical hashcodes and equals

Viewed 74

Maybe not the best Title, please feel free to edit it.

This is the code I have..

import java.util.Map;
import java.util.HashMap;

public class App {
    public static void main(String[] args) {
        final Foo foo = new Foo();
        final Foo bar = new Foo();

        System.out.println("foo equals foo: " + foo.equals(foo));
        System.out.println("foo equals bar: " + foo.equals(bar));

        System.out.println("foo hashcode: " + foo.hashCode());
        System.out.println("bar hashcode: " + bar.hashCode());

        final Map<Foo, Integer> foos = new HashMap<Foo, Integer>();
        foos.put(foo, -99);

        System.out.println("foos.getfoo: " + foos.get(foo));
        System.out.println("foos.getbar: " + foos.get(bar));
    }    
}

class Foo {
    @Override
    public boolean equals(Object o) {
        return false;
    }

    @Override
    public int hashCode() {
        return -1;
    }
}

So before reading further can you guess what the output for the following 2 statement will be?

System.out.println("foos.getfoo: " + foos.get(foo));
System.out.println("foos.getbar: " + foos.get(bar));

I would expect to see:

null
null

since even though the hashCodes do match, equals for any instance of Foo will always return false, so using an instance of Foo as a key in a Map should not be useful at all..

However the output is:

:~ $ javac App.java
:~ $ java App
foo equals foo: false
foo equals bar: false
foo hashcode: -1
bar hashcode: -1
foos.getfoo: -99
foos.getbar: null

What am I missing? How is -99 retrieved when I use an object that has an hashcode -1 and is NOT equal to itself, but then get null later with a same type of instance that is also NOT equal to what I have in the Map and also has hashcode -1?

1 Answers
Related