title: How does a lambda function become the compare() method of Comparator
body: <p>I have seen that in Java 8, one can define a comparator like this:</p>
  
  <pre><code>Comparator c = (Computer c1, Computer c2) -&gt; c1.getAge().compareTo(c2.getAge());
  </code></pre>
  
  <p>which is equivalent to:</p>
  
  <pre><code>Comparator d = new Comparator&lt;Computer&gt; () {
      @Override
      public int compare(Computer c1, Computer c2){
          return c1.getAge().compareTo(c2.getAge());
      }
  };
  </code></pre>
  
  <p>I'd like to understand how this works. In the second example, it is fairly simple: A <code>Comparator</code> object is created with a method <code>compare</code> which performs the comparison by using the <code>compareTo</code> method in the <code>age</code> property of <code>Computer</code>. This method is simply called by us when we do:</p>
  
  <pre><code>Computer comp1 = new Computer(10);
  Computer comp2 = new Computer(11);
  d.compare(comp1, comp2); // -1
  </code></pre>
  
  <p>But what's going on in the first example, when using a lambda? It looks to me like we are setting the <code>Comparator</code> to be equal to a method that performs comparison. But this cannot be, because a <code>Comparator</code> object is an object that has a method <code>compare</code>. I've learned that lambdas can be used with functional interfaces (interfaces with only one method). But <code>Comparator</code> is not a functional interface (it has many other methods other than <code>compare</code>!). So how does the Java interpreter know that it is the <code>compare</code> method we are implementing?</p>