title: Finding out if a list of Objects contains something with a specified field value?
body: <p>I have a list of DTO received from a DB, and they have an ID. I want to ensure that my list contains an object with a specified ID. Apparently creating an object with expected fields in this case won't help because contains() calls for Object.equals(), and they won't be equal.</p>
  <p>I came up to a solution like so: created an interface <code>HasId</code>, implemented it in all my DTOs, and inherited ArrayList with a new class that has <code>contains(Long id)</code> method.</p>
  <pre><code>public interface HasId {
      void setId(Long id);
      Long getId();
  }
  
  public class SearchableList&lt;T extends HasId&gt; extends ArrayList&lt;T&gt; {
      public boolean contains(Long id) {
          for (T o : this) {
              if (o.getId() == id)
                  return true;
          }
          return false;
      }
  }
  </code></pre>
  <p>But in this case I can't typecast List and ArrayList to SearchableList...
  I'd live with that, but wanted to make sure that I'm not inventing the wheel.</p>
  <p>EDIT (Oct '16):</p>
  <p>Of course, with the introduction of lambdas in Java 8 the way to do this is straightforward:</p>
  <pre><code>list.stream().anyMatch(dto -&gt; dto.getId() == id);
  </code></pre>