4

I am having trouble figuring out how to write my equals method using generics as I am sure if using instanceOf is necessary. Im trying to compare that two ItemPairs are the same (logically equivalent) if both items within the pair are the same (logically equivalent)

Here is my attempt at it:

public class ItemPair<T> { 

private T item1; 
private T item2; 

public ItemPair(T item1, T item2) { 
    this.item1 = item1; 
    this.item2 = item2;
}

public T getItem1() {
    return item1;
}

public void setItem1(T item1) {
    this.item1 = item1; 
}

public T getItem2() {
    return item2;
}

public void setItem2(T item2) {
    this.item2 = item2; 
}

@Override
public boolean equals(Object obj) {
    if(obj instanceof T) {
        return this.item1.equals(this.item2);
    } else {
        return false;
    }
}

}
5
  • See this link it may help. Commented May 1, 2017 at 6:47
  • 3
    What you're trying to do is impossible and completely wrong. equals() should be comparing this to obj, which is usually another ItemPair, not item1 to item2. Commented May 1, 2017 at 6:49
  • Thank you for the link and suggestion! Commented May 1, 2017 at 6:51
  • If you're writing the equals method for class ItemPair, the instanceof should be for checking that class, not for T. Look at this class, it may be useful in your case: github.com/unicesi/amelia/blob/master/maven/org.amelia.dsl.lib/… Commented May 1, 2017 at 6:51
  • You can use if (! getClass().isInstance(obj)) {return false;} ItemPair<T> other = getClass().cast(obj);. Commented May 1, 2017 at 7:45

1 Answer 1

5

Your equals method should not determine if the first item is equal to the second item of the same ItemPair object. It should determine if two ItemPairs are equal to each other:

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (!(obj instanceof ItemPair))
        return false;
    IterPair other = (IterPair) obj;
    return this.item1.equals(other.item1) && this.item2.equals(other.item2);
}

Some additional conditions may be required for the cases where either of the items are null.

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

5 Comments

if (obj == null) is redundant. instanceof will anyway return false.
And if you don't want to have subclasses count as equal via instanceof, then you can compare #getClass (which is where the null check would be necessary)
While the (obj == null) is redundant, there is a good chance that the JIT compiler will optimize it away.
Is if (!(obj instanceof ItemPair)) could not be if (!(obj instanceof ItemPair<?>)) to avoid warning ? Here you use a raw ItemPair.
return this.item1.equals(other.item1) && this.item2.equals(other.item2); ==> return Objects.equals(this.item1, other.item1) && Objects.equals(this.item2, other.item2); to handle the null case.

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.