0

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;
    }

}
user697911
  • 10,043
  • 25
  • 95
  • 169
  • So what exactly is the question? – Mureinik May 22 '17 at 21:29
  • 1
    Sort of. If you implement `CompareTo` but don't override `equals`, you may end up with some situations where `a.compareTo(b) == 0` but `a.equals(b) == false`. Which is obviously confusing. And as usual, if you override `equals`, you should override `hashCode`. – Michael May 22 '17 at 21:33

1 Answers1

1

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.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • So I better remove euqals and hashcode in this case, right? – user697911 May 22 '17 at 21:44
  • I don't see a reason to. They don't seem to be broken, and having them in place *is* a good idea when working with collections. – Makoto May 22 '17 at 21:47
  • From what you see, is my compareTo implementation is consistent to the generated equals and hashCode? – user697911 May 22 '17 at 21:50
  • A type that implements `Comparable` should (effectively "must") make `compareTo` consistent with `equals`, as pointed out here. If `compareTo` returns `0` for two non-identical objects (i.e., `!=` pertains), and one has not overridden `equals` and `hashCode` consistently with that, then one has made a bad mistake. Read _Effective Java_ for more on this. – Lew Bloch May 22 '17 at 22:12
  • Are the above implementations consistent? – user697911 May 22 '17 at 22:24
  • @user697911: I'm going to leave that as an exercise for the reader. It's not hard to test; equivalence is well-defined for `compareTo`. – Makoto May 22 '17 at 22:25