title: Find most recent date in a list of objects on LocalDate property using Java 8 stream
body: <p>I have a list of objects which hold multiple properties, one of which is a LocalDate. I'd like to find the object with the most recent date out of this list.</p>
  
  <p>I'm fairly green with Java 8 and using streams. Like most programming, it seems as though there's more than one way to skin this cat. Here's what I have so far.</p>
  
  <pre><code>list.stream().filter( object -&gt; object.getId() == id
  &amp;&amp; object.getCancelDate() == null 
  &amp;&amp; object.getEarliestDate() != null)
  .min( Comparator.comparing( LocalDate::toEpochDay )
  .get();
  </code></pre>
  
  <p>This gives me "Non-static method cannot be referenced from a static context" for the Comparator function. </p>
  
  <p>I've looked at possibly creating a map of just the dates from the filtered objects as well and have so far come up with something like this.</p>
  
  <pre><code>list.stream().filter( object -&gt; object.getId() == id
  &amp;&amp; object.getCancelDate() == null 
  &amp;&amp; object.getEarliestDate() != null)
  .map( data -&gt; data.getEarliestDate() )
  .collect( Collectors.toList() )
  </code></pre>
  
  <p>and I'm not really sure where to go from there or if that will even work. </p>
  
  <p>I know there's an easy way to do this but my brain just isn't connecting the dots.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks for the response. I updated my code
       <code>Optional&lt;YourObject&gt; recentObject = list.stream().filter(object -&gt; 
       object.getId() == id &amp;&amp; object.getCancelDate() == null &amp;&amp; 
       object.getEarliestDate() != null)
      .max(Comparator.comparing(s -&gt; s.getEarliestDate().toEpochDay()));</code></p>
  
  <p>I now get a compiler error 
  Incompatible types.</p>
  
  <pre><code>Required:Optional&lt;MyClass&gt;
  Found:Optional&lt;capture&lt;? extends MyClass&gt;&gt;
  </code></pre>
  
  <p>The method does extend MyClass, so in the type declaration for Optional, do I need to do something like MyClass.class? </p>
  
  <p><strong>Update 2</strong>
  Thanks to @Hogen for helping fix the compiler error by adding on the .map() at the end. Here's what it looked like after the change.</p>
  
  <pre><code>Optional&lt;MyClass&gt; date = 
  list.stream().filter(object -&gt; object.getId() == id &amp;&amp;
  object.getCancelDate() == null &amp;&amp;
  object.getEarliestDate() != null)
  .max(Comparator.comparing( s -&gt; s.getEarliestDate()
  .toEpochDay())).map(Function.identity());
  </code></pre>
  
  <p>However, I was able to come up with a solution after some help that moves the map to a different spot so that I wouldn't run into the issue of using an extended class.</p>
  
  <pre><code>Optional&lt;LocalDate&gt; mostRecentDate = list.stream()
  .filter(data -&gt; data.getId() == id &amp;&amp; data.getCancelDate() == null)
  .map(MyObject::getEarliestDate)
  .filter(Objects::nonNull)
  .max(LocalDate::compareTo);
  </code></pre>