Equals implementation in a java-class which implements an interface that extends Iterable?
<p>How would one implement equals in a java-class which implements an interface that extends Iterable?</p>

<p>The interface</p>

<pre><code>public interface MyInterface extends Iterable&lt;String&gt; {
...
}
</code></pre>

<p>The concrete class</p>

<pre><code>public class MyClass implements MyInterface {

  private Set&lt;String&gt; myStrings = new HashSet&lt;String&gt;();

  @Override
  public Iterator&lt;String&gt; iterator() {
    return myStrings.iterator();
  }

  @Override
  public boolean equals(Object otherObject) {
</code></pre>

<p>How should I check that both this instance and the other instances contains the same set of strings? The easy way would to only check equals against this implementation and not the interface, but that sounds like cheating.</p>

<pre><code>    if (otherObject instanceof MyClass) { ... } // easy, just myStrings.equals(...)
</code></pre>

<p>but</p>

<pre><code>    if (otherObject instanceof MyInterface) { ... } // compare two iterators?
</code></pre>

<p>Or am I missing something? I must implement hashCode aswell, and if two objects are equal should not their hashcodes be identical hence equals must only check against MyClass to fullfil this contract?!</p>

<pre><code>  }

}
</code></pre>
