title: Maximum Frequency Stack Question on LeetCode
body: <p>I'm trying to solve the maximum frequency stack question on leetcode using a combination of a treeset and a hashMap. My problem is that even with the comparable interface for my custom class, the contains function for treeset does not seem to work properly. It adds multiple items of the same value.</p>
  
  <p>Where am i going wrong?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>class FreqStack {
      private int size = 0;
      HashMap&lt;Integer, Element&gt; map;
      TreeSet&lt;Element&gt; set;
  
      public FreqStack() {
          map = new HashMap&lt;Integer, Element&gt;();
          set = new TreeSet&lt;Element&gt;();
      }
  
      public void push(int x) {
          size++;
          Element elem = map.get(x);
          if (elem == null) {
              // System.out.println("creating new: " + x);
              elem = new Element(x);
          }
          elem.add(size);
          map.put(x, elem);
          if (set.contains(elem)) {
              System.out.println("set contains: " + elem.val);
              set.remove(elem);
          }
          set.add(elem);
  
          System.out.println(set.stream().map(v -&gt; v.val).collect(Collectors.toList()));
      }
  
      public int pop() {
          // size--;
          Element elem = set.pollFirst();
          System.out.println(elem.val);
          elem.remove();
  
          if (elem.getFreq() &gt; 0) {
              set.add(elem);
          } else {
              map.remove(elem.val);
          }
  
          System.out.println(set.stream().map(v -&gt; v.val).collect(Collectors.toList()));
          return elem.val;
      }
  
      class Element implements Comparable&lt;Element&gt; {
          int val;
          LinkedList&lt;Integer&gt; rank;
  
          Element(int val) {
              this.val = val;
              this.rank = new LinkedList&lt;Integer&gt;();
          }
  
          void add(int count) {
              System.out.println("Adding " + val + " - count: " + count);
              rank.add(count);
          }
  
          void remove() {
              System.out.println("Removing: " + rank.size());
              rank.pollLast();
          }
  
          int getRank() {
              return rank.peekLast();
          }
  
          int getFreq() {
              return rank.size();
          }
  
          @Override
          public int compareTo(Element e) {
              System.out.println("Comparing: " + val + " and " + e.val);
              if (val == e.val)
                  return 0;
  
              if (getFreq() == e.getFreq()) {
                  return e.getRank() - getRank();
              }
  
              return e.getFreq() - getFreq();
          }
          @Override
          public int hashCode() {
              return val;    
          }
  
          @Override
          public boolean equals(Object o) {
             return (o instanceof Element) &amp;&amp; (this.compareTo((Element) o) == 0);
          }
      }
  }
  
  /**
   * Your FreqStack object will be instantiated and called as such:
   * FreqStack obj = new FreqStack();
   * obj.push(x);
   * int param_2 = obj.pop();
   */
  </code></pre>
  
  <p>Here's the part output. Note how it adds 5 twice:</p>
  
  <pre><code>Adding 5 - count: 1
  Comparing: 5 and 5
  [5]
  Adding 1 - count: 2
  Comparing: 1 and 5
  Comparing: 1 and 5
  [1, 5]
  Adding 2 - count: 3
  Comparing: 2 and 5
  Comparing: 2 and 1
  Comparing: 2 and 5
  Comparing: 2 and 1
  [2, 1, 5]
  Adding 5 - count: 4
  Comparing: 5 and 1
  Comparing: 5 and 2
  Comparing: 5 and 1
  Comparing: 5 and 2
  [5, 2, 1, 5]
  </code></pre>