title: compareTo with objects returns a false while it is true
body: <p>I am trying to check whether my levelorder of my Binary Search Tree is equal to the other one. To do this, I tried to make a compareTo method. I only give equal values to the method, but it keeps on saying the condition is false. When I place breakpoints, I see that the values are still equal. I am probably not understanding it correctly. Does anyone know how to solve this?</p>
  <p>Here is what I did, as you can see below, the compareTo returns a 1 instead of a 0:</p>
  <pre><code>import edu.princeton.cs.algs4.BST;
  import java.util.*;
  
  public class MyBST implements Comparable&lt;MyBST&gt;{
  
      private Object e;
  
      public MyBST(Object e){
          this.e = e;
      }
  
      private Object getE(){
          return e;
      }
  
      public static void main(String[] args) {
  
          int size = 4;
  
          Random r = new Random();
          Set&lt;Integer&gt; tes = new LinkedHashSet&lt;&gt;(size);
          Stack&lt;Integer&gt; stack = new Stack&lt;&gt;();
  
          while (tes.size() &lt; size) {
              tes.add(r.nextInt(10));
          }
  
          System.out.println(&quot;possible combinations&quot;);
          Set&lt;Stack&lt;Integer&gt;&gt; combos = combos(tes, stack, tes.size());
  
          Object[] arr = combos.toArray();
          List&lt;String&gt; d = new ArrayList&lt;&gt;();
          for (Object s : arr) {
              String b = s.toString();
              b = b.replaceAll(&quot;\\[&quot;, &quot;&quot;).replaceAll(&quot;\\]&quot;, &quot;&quot;);
              d.add(b);
          }
  
          int index = 0;
  
          do {
              BST&lt;String, Integer&gt; bst1 = new BST&lt;String, Integer&gt;();
              BST&lt;String, Integer&gt; bst2 = new BST&lt;String, Integer&gt;();
              String key1 = d.get(index);
              String key2 = d.get(index);
              key1 = key1.replaceAll(&quot; &quot;, &quot;&quot;);
              String[] m = key1.split(&quot;,&quot;);
  
              key2 = key2.replaceAll(&quot; &quot;, &quot;&quot;);
              String[] n = key2.split(&quot;,&quot;);
  
              System.out.println(&quot;1e order&quot;);
              for (int j = 0; j &lt; m.length; j++) {
  
                  System.out.println(m[j]);
                  bst1.put(m[j], 0);
              }
  
              System.out.println(&quot;2e order&quot;);
              for (int j = 0; j &lt; n.length; j++) {
  
                  System.out.println(n[j]);
                  bst2.put(n[j], 0);
              }
  
              System.out.println(&quot;levelorder 1e BST&quot;);
  
              MyBST e = new MyBST(bst1.levelOrder());
              MyBST y = new MyBST(bst2.levelOrder());
  
              System.out.println(bst1.levelOrder());
  
              System.out.println(&quot;levelorder 2e BST&quot;);
  
              System.out.println(bst2.levelOrder());
  
              System.out.println(e.compareTo(y) + &quot;\n&quot;);
              index++;
          } while (index &lt; arr.length - 1);
  
      }
      public static Set&lt;Stack&lt;Integer&gt;&gt; combos(Set&lt;Integer&gt; items, Stack&lt;Integer&gt; stack, int size) {
          Set&lt;Stack&lt;Integer&gt;&gt; set = new HashSet&lt;&gt;();
  
          if (stack.size() == size) {
              set.add((Stack) stack.clone());
          }
          Integer[] itemz = items.toArray(new Integer[0]);
          for (Integer i : itemz) {
              stack.push(i);
              items.remove(i);
              set.addAll(combos(items, stack, size));
              items.add(stack.pop());
          }
          return set;
      }
  
      @Override
      public int compareTo(MyBST o) {
          if (this.e == o.e) {
              return 0;
          }
          else
              return 1;
      }
  }
  </code></pre>
  <p>Here you can find the BST.java class: <a href="https://algs4.cs.princeton.edu/32bst/BST.java.html" rel="nofollow noreferrer">BST.java</a></p>
  <p>And the output is something like:</p>
  <p><a href="https://i.stack.imgur.com/irtKk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/irtKk.png" alt="enter image description here" /></a></p>
  <p>The breakpoint at the compareTo method says:</p>
  <p><a href="https://i.stack.imgur.com/17Y2G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/17Y2G.png" alt="enter image description here" /></a></p>