title: Java 8 Stream to find element in list
body: <p>I have the following class:</p>
  
  <pre><code>public class Item {
      int id;
      String name;
      // few other fields, contructor, getters and setters
  }
  </code></pre>
  
  <p>I have a list of Items. I want to iterate through the list and find the instance which has a particular id. I'm trying to do it through streams.</p>
  
  <pre><code>public void foobar() {
  
      List&lt;Item&gt; items = getItemList();
      List&lt;Integer&gt; ids = getIdsToLookup();
      int id, i = ids.size() - 1;
  
      while (i &gt;= 0) {
          id = ids.get(i);
          Optional&lt;Item&gt; item = items
              .stream()
              .filter(a -&gt; a.getId() == id)
              .findFirst();
          // do stuff
          i--;
      }
  }
  </code></pre>
  
  <p>Is this the best way to iterate over the list and get the element I need? Also, I get an error on the filter line for id which says variables used in lambda expressions must be final or effectively final. Maybe I can define id inside the while loop, that should get rid of the exception. Thanks.</p>