Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Look here:</p> <pre><code>public boolean equals(Object other) { if (this.getClass().isInstance(other)) { return true; } else { return false; } } </code></pre> <p>Your <code>equals()</code> method is definitely broken. This returns <code>true</code> for <strong>every</strong> other <code>User</code> object, even though it internally holds a completely different user ID/name. So the selected item matches <strong>every</strong> available select item value. That's why you see the last item being preselected everytime.</p> <p>Assuming that the <code>id</code> property is unique for every user, then the <code>equals()</code> method should at its simplest rather look like this:</p> <pre><code>public boolean equals(Object other) { if (!(other instanceof User)) { return false; } if (other == this) { return true; } if (id != null) { return id.equals(((User) other).id); } return false; } </code></pre> <p>which can also be summarized as follows</p> <pre><code>public boolean equals(Object other) { return (other instanceof User) &amp;&amp; (id != null) ? id.equals(((User) other).id) : (other == this); } </code></pre> <p>Hint: a bit decent IDE like Eclipse can autogenerate <code>equals()</code> (and <code>hashCode()</code>) method.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/3181339/right-way-to-implement-equals-contract">Right way to implement equals contract</a></li> <li><a href="https://stackoverflow.com/questions/4417035/generic-reflective-helper-method-for-equals-and-hashcode/4417048#4417048">Generic reflective helper method for equals and hashCode</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload