title: Java HashSet contains returns false, even with overridden equals() and hashCode()
body: <p>I initialize the HashSet like this:</p>
  
  <pre><code>private HashSet&lt;Rule&gt; ruleTable = new HashSet&lt;Rule&gt;();
  </code></pre>
  
  <p>The <code>equals()</code> and <code>hashCode()</code> methods of my <code>TcpRule</code> object (sub-class of abstract class <code>Rule</code>) look like this:</p>
  
  <pre><code>@Override
  public int hashCode() {
      // Ignore source Port for now
      return (this.getSrcPool() + ":" + this.getDstPool() + ":" + this.getProtocol() + ":" + this.dstTcp).hashCode();
  }
  
  @Override
  public boolean equals(Object obj) {
      if (!(obj instanceof TcpRule))
          return false;
      if (obj == this)
          return true;
  
      TcpRule r = (TcpRule) obj;
      return (this.getSrcPool().equals(r.getSrcPool()) &amp;&amp; this.getDstPool().equals(r.getDstPool()) &amp;&amp; this.getProtocol().equals(r.getProtocol()) &amp;&amp; this.getSrcTcp() == r.getSrcTcp() &amp;&amp; this.getDstTcp() == r.getDstTcp());
  }
  </code></pre>
  
  <p>I have even written a simple unit test, which does not give any error:</p>
  
  <pre><code>@Test
  public void equalsTest() {
      Pool srcPool = new Pool("PROXY");
      Pool dstPool = new Pool("WEB");
      int srcTcp = 54321;
      int dstTcp = 80;
  
      TcpRule r1 = new TcpRule(srcPool, dstPool, srcTcp, dstTcp);
      TcpRule r2 = r1;
      assert r1.equals(r2);
  
      TcpRule r3 = new TcpRule(srcPool, dstPool, srcTcp, dstTcp);
      TcpRule r4 = new TcpRule(srcPool, dstPool, srcTcp, dstTcp);
      assert r3.equals(r4);
  }
  
  @Test
  public void hashCodeTest() {
      Pool srcPool = new Pool("PROXY");
      Pool dstPool = new Pool("WEB");
      int srcTcp = 54321;
      int dstTcp = 80;
  
      TcpRule r1 = new TcpRule(srcPool, dstPool, srcTcp, dstTcp);
      TcpRule r2 = new TcpRule(srcPool, dstPool, srcTcp, dstTcp);
      assert r1.hashCode() == r2.hashCode();
  
      HashSet&lt;Rule&gt; rules = new HashSet&lt;Rule&gt;();
      rules.add(r1);
      assert rules.contains(r1);
  
      assert rules.contains(r2);
  }
  </code></pre>
  
  <p>In my application, I have an <code>add()</code> method where I simply add a <code>Rule</code> object to the <code>HashSet</code>:</p>
  
  <pre><code>@Override
  public void add(Rule rule) {
      ruleTable.add(rule);
  }
  </code></pre>
  
  <p>In another method, I check if a rule exists in the <code>HashSet</code>:</p>
  
  <pre><code>    @Override
  public boolean isPermittedTcp(IpAddress sourceAddress, IpAddress destinationAddress, short srcTcp, short dstTcp) {
      Pool sourcePool = poolService.getPool(new Host(sourceAddress));
      Pool destinationPool = poolService.getPool(new Host(destinationAddress));
      Rule r = new TcpRule(sourcePool, destinationPool, srcTcp, dstTcp);
      log.info("Checking: " + r.toString());
      log.info("Hash-Code: " + r.hashCode());
      log.info("Hashes in ruleTable:");
      for(Rule rT : ruleTable) {
          log.info("" + rT.hashCode());
      }
      if(ruleTable.contains(r)) {
          log.info("Hash found!");
      } else {
          log.info("Hash not found!");
      }
      return ruleTable.contains(r);
  }
  </code></pre>
  
  <p>The log messages indicate that the hash of the <code>Rule</code> object (<code>r.hashCode()</code>) is <code>-1313430269</code>, and that one hash in the <code>HashSet</code> (<code>rT.hashCode()</code> in the loop) is also <code>-1313430269</code>. 
  But <code>ruleTable.contains(r)</code> always returns <code>false</code>. What am I doing wrong?</p>
  
  <p>I have found similar questions on StackOverflow, but these mostly involve the <code>equals()</code> or <code>hashCode()</code> methods not being (correctly) overridden. I think I have implemented this two methods correctly. </p>