4

I have Map<Date, String. I have two Date objects a, b that are equal. I put a string value to the map associated with key a. Then I try to get map value associated with keys a and b but only a returns value that I've put. Is it possible to get my value with b key. I know this is possible when keys are simple strings. Why this doesn't work with other type of objects?

public class Main {

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

    Date a = new Date(20131105);
    Date b = new Date(20131105);

    map.put(a, "sweet");

    System.out.println(map.get(a));
    System.out.println(map.get(b));
}

static class Date {
    private int ymd;

    public Date(int ymd) {
        this.ymd = ymd;
    }

    public int getYmd() {
        return ymd;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Date) {
            return ((Date) obj).ymd == ymd;
        }
        return false;
    }
}

}

The output is:

sweet
null
0

4 Answers 4

5

Since you're using an HashMap for storing your date objects, you have to override the hashCode() method because the key objects are stored in the data structure using their hashCode.

Very basic hashCode() overriden (just for illustration):

@Override
public int hashCode(){
   return ymd;
}

Output :

sweet
sweet

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

3 Comments

@RichardTingle He's using his own Date class.
Just noticed that, people need to stop doing that
@RichardTingle I hope this is only for an exercise purpose !!
2

A hashmap hashes the elements using their hashCode function. For most types the hash code is computed using the reference of the object and this is the case with Date. While in your case the two dates have same value, they are not the same object. Thus they have different references and so their hash codes differ. When you lookup an element in a HashMap its hashCode is used and so as b's hashCode is different from a's hashCode you can not find an element with key a by using b.

Comments

1

You need to implement hashCode() method in your Date class

Add below code in your Date class

    @Override
    public int hashCode() {              
          return ymd;
    }

output

sweet
sweet

Comments

0

Because String,Integer and all wrapper class override hasCode and equals both the method. But In your case equals() method will return true but you have not override hasCode() method so it will generate different hashcode for both a and b .So map.get(b) will return null value.

3 Comments

I dont understand why are you creating a separate date class for Date use directly java.util.Date; Map<Date, String> map = new HashMap<Date, String>(); Date a = new Date(20131105); Date b = new Date(20131105); map.put(a, "sweet"); System.out.println(map.get(a)); System.out.println(map.get(b)); Output: sweet sweet Because java.util.Date class override equals and hashCode method.
I wrote this code just as a sample for my question. I don't use anywhere else.
Please give proper naming convention.

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.