title: Java 8 mapToInt and toIntFunction examples
body: <p>I am testing the new major update from Java (Java 8) and it is very interesting. I am using streams; in particular I am using this simple code.</p>
  <pre><code>private void getAvg()
  {
      final ArrayList&lt;MyPerson&gt;persons = new ArrayList&lt;&gt;
      (Arrays.asList(new MyPerson(&quot;Ringo&quot;,&quot;Starr&quot;),new MyPerson(&quot;John&quot;,&quot;Lennon&quot;),new MyPerson(&quot;Paul&quot;,&quot;Mccartney&quot;),new MyPerson(&quot;George&quot;,&quot;Harrison&quot;)));                
      final OptionalDouble average = persons.stream().filter(p-&gt;p.age&gt;=40).mapToInt(p-&gt;p.age).average();
      average.ifPresent(System.out::println);        
      return;
  }            
  private class MyPerson
  {
      private final Random random = new Random();
      private final String name,lastName;
      private int age;
      public MyPerson(String name,String lastName){this.name = name;this.lastName = lastName;this.age=random.nextInt(100);}    
      public MyPerson(String name,String lastName,final int age){this(name,lastName);this.age=age;}    
      public String getName(){return name;}
      public String getLastName(){return lastName;}                   
      public int getAge(){return age;}        
  }            
  </code></pre>
  <p>In this example I understand very clear but later I have seen also can accomplish it in this way:</p>
  <pre><code>final OptionalDouble average = persons.stream().filter(p-&gt;p.age&gt;=40)
  .mapToInt(MyPerson::getAge).average();
  average.ifPresent(System.out::println);        
  </code></pre>
  <p>I have checked the method <strong>toIntFunction</strong> and in fact have the following signature:</p>
  <pre><code>@FunctionalInterface
  public interface ToIntFunction&lt;T&gt; {
  
  /**
   * Applies this function to the given argument.
   *
   * @param value the function argument
   * @return the function result
   */
  int applyAsInt(T value);
  }
  </code></pre>
  <p>As I can see the applyAsInt have a input and returns an int as long as I understand.</p>
  <p>This code:</p>
  <pre><code>MyPerson::getAge
  </code></pre>
  <p>calls:</p>
  <pre><code>public int getAge(){return age;}//please correct me at this point        
  </code></pre>
  <p>My question is: the method <code>getAge</code> have not parameters and returns a <strong>int</strong> but the <em>toIntFunction</em> receive a parameter - this is the part I don't understand.</p>
  <p>The parameter from <code>toIntFunction</code> is inferred or something.</p>