title: Why does Set.contains() not seem to be using o.equals()?
body: <p>I have a TreeSet containing wrappers which store a <code>Foo</code> object at a certain <code>position</code>, defined like so:</p>
  
  <pre><code>class Wrapper implements Comparable&lt;Wrapper&gt; {
    private final Foo foo;
    private final Double position;
  
    ...
  
    @Override boolean equals(Object o) {
  
      ... 
  
      if(o instanceof Wrapper)
          return o.getFoo().equals(this.foo);
  
      if(o instanceof Foo)
          return o.equals(this.foo);
    }
  
    @Override public int compareTo(MarkerWithPosition o) {
        return position.compareTo(o.getPosition());
    }
  }
  
  NavigableSet&lt;Wrapper&gt; fooWrappers = new TreeSet&lt;Wrapper&gt;();
  </code></pre>
  
  <p>because I want my <code>TreeSet</code> to be ordered by <code>position</code> but searchable by <code>foo</code>. But when I perform these operations:</p>
  
  <pre><code>Foo foo = new Foo(bar);
  Wrapper fooWrapper = new Wrapper(foo, 1.0);
  fooWrappers.add(fooWrapper);
  
  fooWrapper.equals(new Wrapper(new Foo(bar), 1.0));
  fooWrapper.equals(new Foo(bar));
  fooWrappers.contains(fooWrapper);
  fooWrappers.contains(new Wrapper(foo, 1.0));
  fooWrappers.contains(new Wrapper(new Foo(bar), 1.0));
  fooWrappers.contains(new Wrapper(foo, 2.0));
  fooWrappers.contains(foo);
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>true
  true
  true
  true
  true
  false
  Exception in thread "main" java.lang.ClassCastException: org.gridqtl.Marker cannot be cast to java.lang.Comparable
      at java.util.TreeMap.getEntry(TreeMap.java:325)
      at java.util.TreeMap.containsKey(TreeMap.java:209)
      at java.util.TreeSet.contains(TreeSet.java:217)
  </code></pre>
  
  <p>when I expecting them all to return <code>true</code>, so it seems like <code>TreeSet.contains</code> is not using my <code>equals</code> method as the API <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Set.html#contains%28java.lang.Object%29" rel="noreferrer">suggests</a>. Is there another method I need to overwrite?</p>