title: Custom object comparator
body: <p>I will try to get right to the point.</p>
  
  <p>I am having my custom Node objects, which have attribute Cost. I would want to sort those Node objects in ascending order by their attribute Cost.</p>
  
  <p>I was able to do so using <code>PriorityQueue&lt;Node&gt; = new PriorityQueue&lt;Node&gt;(10000, new NodeComparator());</code>, but that way worked too slow for me, and now I am looking to do the same thing, only using TreeSet.
  Anyways, if my constructor looks like this <code>TreeSet&lt;Node&gt; = new TreeSet&lt;Node&gt;(new NodeComparator());</code>, the program seems to skip vast amount of Node objects, seemingly treating them as they are the same. Which they are not. I am assuming there might be some hashCode issues about, but I am not sure, and I don't know how to resolve it at this moment.</p>
  
  <p>To be concise, I just want my Nodes in TreeSet to be ordered in ascending way by Cost attribute.
  Here is my NodeComparator class:</p>
  
  <pre><code>public class NodeComparator implements Comparator&lt;Node&gt; {
  
      @Override
      public int compare(Node n1, Node n2) {
          // TODO Auto-generated method stub
          if(n1.cost &gt; n2.cost) return 1;
          else if(n1.cost &lt; n2.cost) return -1;
          else return 0;
      }
  
  }
  </code></pre>
  
  <p>And here is my Node class:</p>
  
  <pre><code>public class Node{
  
      public State state;
      public int cost;
  
      public Node(State s, int Cost){
          this.state = s;
          this.cost = Cost;
      }
  
      public State getState(){
  
          return this.state;
      }
  
      public int getCost(){
          return this.cost;
      }
  }
  </code></pre>
  
  <p>I will provide you with my State class aswell.</p>
  
  <pre><code>public class State {
  
      public int lamp;
  
      public ArrayList&lt;Integer&gt; left;
  
  
      public State(ArrayList&lt;Integer&gt; Left, int Lamp){
          lamp = Lamp;
          left = Left;
      }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + lamp;
          result = prime * result + ((left == null) ? 0 : left.hashCode());
          return result;
      }
  
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          State other = (State) obj;
          if (lamp != other.lamp)
              return false;
          if (left == null) {
              if (other.left != null)
                  return false;
          } else if (!left.equals(other.left))
              return false;
          return true;
      }
  }
  </code></pre>