1

I have a hashmap of two objects, but it seems to return different values because the hashcode is different. Ex.

HashMap<HashMapTest, String> newMap = new HashMap<HashMapTest, String>();
    newMap.put(new HashMapTest("test"), "line");
    System.out.println(newMap.get(new HashMapTest("test")));

the hashcodes are different when I put it in the hashmap and when I get it from the hashmap. Is there any way of fixing this?

1
  • 1
    Let's see your HashMapTest class please. Commented Apr 21, 2012 at 16:37

1 Answer 1

6

Did you provide your custom implementation of the hashCode() method in HashMapTest? I guess not, and that's the default behavior of hashCode() inherited from java.lang.Object (you are actually using two different objects.) Have a look at this question to provide correct hashCode/equals implementations for your classes.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.