title: set does not contain an item that equals one of its members?
body: <p>So according to <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Set.html" rel="nofollow noreferrer">the Java API</a>, <code>Set::contains</code></p>
  
  <blockquote>
    <p>returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e))</p>
  </blockquote>
  
  <p>So ... why is it this method returns false even IF the set contains exactly one element that is equal to <code>creds</code>?</p>
  
  <pre><code>private boolean validate(Credentials creds){
      Set&lt;Credentials&gt; acceptedUsers = getAcceptedUsers();
      return acceptedUsers.contains(creds);
  }
  </code></pre>
  
  <p>More specifically, I inserted some <code>printlns</code> </p>
  
  <pre><code>private boolean validate(Credentials creds){
      Set&lt;Credentials&gt; acceptedUsers = getAcceptedUsers();
  
      System.out.print("accepted users: ");
      System.out.println(acceptedUsers);
      System.out.print("accessing user: ");
      System.out.println(creds);
      System.out.println("items are equal: " + acceptedUsers.stream().map(c -&gt; c.equals(creds)).collect(Collectors.toSet()));
      System.out.println("access ok: " + (acceptedUsers.contains(creds) ? "YES" : "NO"));
  
      return acceptedUsers.contains(creds);
  }
  </code></pre>
  
  <p>and this is what it had to say: </p>
  
  <pre><code>accepted users: [[
      foo
      FCDE2B2EDBA56BF408601FB721FE9B5C338D10EE429EA04FAE5511B68FBF8FB9
  ]]
  accessing user: [
      foo
      FCDE2B2EDBA56BF408601FB721FE9B5C338D10EE429EA04FAE5511B68FBF8FB9
  ]
  items are equal: [true]
  access ok: NO
  </code></pre>
  
  <p><code>getAcceptedUsers</code> currently returns a dummy set</p>
  
  <pre><code>private Set&lt;Credentials&gt; getAcceptedUsers(){
      return new HashSet&lt;Credentials&gt;(){{add(new Credentials("foo","bar", false));}};
  }
  </code></pre>
  
  <p>and <code>Credentials</code> is implemented as</p>
  
  <pre><code>class Credentials{
      final String username;
      final String password;
  
      public Credentials(String username, String password, boolean isPasswordHashed) {
          this.username = username;
  
          if(isPasswordHashed) this.password = password;
          else {
              MessageDigest md;
              try {
                  md = MessageDigest.getInstance("SHA-256");
              } catch (NoSuchAlgorithmException e) {
                  throw new IllegalStateException(e);
              }
  
              md.update(password.getBytes());
              byte[] hash = md.digest();
  
              this.password = (new HexBinaryAdapter()).marshal(hash);
          }
      }
  
      @Override
      public boolean equals(Object obj) {
          if(obj == null) return false;
          if(!(obj instanceof Credentials)) return false;
          Credentials other = (Credentials)obj;
          return this.username.equals(other.username) &amp;&amp; this.password.equals(other.password);
      }
  
      @Override
      public String toString() {
          return String.format("[\n\t%s\n\t%s\n]", username,password);
      }
  }
  </code></pre>