Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your <code>Pair</code> class needs to implement <code>equals()</code> and <code>hashCode()</code> and you're all set. <code>List.contains()</code> is implemented in terms of the type's <code>equals()</code> method. See the <a href="http://download.oracle.com/javase/6/docs/api/java/util/List.html#contains%28java.lang.Object%29" rel="nofollow noreferrer">API for <code>List.contains()</code></a>. (Edited a bit to address comments from @maaartinus, whose answer you should read b/c the observations are solid, and it's a bit ridiculous for me to fold them in here. As maaartinus points out, a best-practice here would be to avoid error-prone manual definitions for equals and hashcode, and instead build on Guava's helper functions for <a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Objects.html#equal%28java.lang.Object,%20java.lang.Object%29" rel="nofollow noreferrer">nullable equals</a> and <a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Objects.html#hashCode%28java.lang.Object...%29" rel="nofollow noreferrer">hashCode for n objects</a>).</p> <pre><code>final class Pair&lt;T&gt; { final T left; final T right; public Pair(T left, T right) { if (left == null || right == null) { throw new IllegalArgumentException("left and right must be non-null!"); } this.left = left; this.right = right; } public boolean equals(Object o) { // see @maaartinus answer if (! (o instanceof Pair)) { return false; } Pair p = (Pair)o; return left.equals(p.left) &amp;&amp; right.equals(p.right); } public int hashCode() { return 7 * left.hashCode() + 13 * right.hashCode(); } } </code></pre> <p>With suitable <code>equals()</code>, you can now do:</p> <pre><code> lp.add(new Pair("1", "2")); assert lp.contains(new Pair("1","2")); </code></pre> <p>Responding to the comments below, perhaps it would be good to include a good reference for "Why do I need to implement <code>hashCode()</code>?"</p> <ul> <li><p>JavaPractices.com &mdash; <a href="http://www.javapractices.com/topic/TopicAction.do?Id=17" rel="nofollow noreferrer">Implementing <code>equals()</code></a> &mdash; "if you override equals, you must override hashCode"</p></li> <li><p><a href="http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals%28java.lang.Object%29" rel="nofollow noreferrer"><code>Object.equals()</code> contract as defined in the API documentation</a></p></li> <li><p><a href="https://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java/27609#27609">StackOverflow answer</a></p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload