user697911
user697911

Reputation: 10551

Do I need to override also hashCode and equals methods with compareTo method?

I want the FeatureEntryKey used as a Map Key, which depends on the two strings. I remember by default String values can handle equality well, so there is no need to auto-generate the two methods here. Is that true?

public class FeatureEntryKey implements Comparable<FeatureEntryKey> {

    private String my_key;
    private String my_pos;

    public FeatureEntryKey(String key, String pos) {
        my_key = key;
        my_pos = pos;

    }

    String getKey() {
        return my_key;
    }

    String getPos() {
        return my_pos;
    }

    @Override
    public int compareTo(FeatureEntryKey entryKey) {
        int key = my_key.compareTo(entryKey.my_key);

        return key == 0 ? this.my_pos.compareTo(entryKey.my_pos) : key;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((my_key == null) ? 0 : my_key.hashCode());
        result = prime * result + ((my_pos == null) ? 0 : my_pos.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        FeatureEntryKey other = (FeatureEntryKey) obj;
        if (my_key == null) {
            if (other.my_key != null)
                return false;
        } else if (!my_key.equals(other.my_key))
            return false;
        if (my_pos == null) {
            if (other.my_pos != null)
                return false;
        } else if (!my_pos.equals(other.my_pos))
            return false;
        return true;
    }

}

Upvotes: 0

Views: 912

Answers (1)

Makoto
Makoto

Reputation: 106460

No; you don't have to, but you really should.

It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.

However, that has nothing to do with whether or not String can handle equality well. That solely has to do with the fact that you're implementing Comparable; you're not tacitly expected to override either equals or hashCode when implementing Comparable.

If you planned on using this in a collection where either equality or hashing were a factor, then you would want to override these, just to ensure that the behavior you get is the behavior you expect.

Upvotes: 1

Related Questions