title: Java 8 Streams map API - interpretation of method reference
body: <p>Sample code:</p>
  
  <pre><code>class Outer {
      public Integer i;
  
      Outer(Integer i) {
          this.i = i;
      }
  
      public int getVal() { return i; }
  }
  
  class MyClass {
  
      public Integer f(Outer o) { return o.getVal();};
  
      public void main() {
  
          MyClass g = new MyClass();
  
          List&lt;Integer&gt; l1 = Arrays.asList(new Outer(2)).stream().map(g::f).collect(Collectors.toList());
          List&lt;Integer&gt; l2 = Arrays.asList(new Outer(2)).stream().map(Outer::getVal).collect(Collectors.toList());
      }
  }
  </code></pre>
  
  <p>Using either of the method references of </p>
  
  <ol>
  <li><p><code>Outer::instanceMethod</code> that takes no argument and is basically a <code>Supplier&lt;T&gt;</code> functional interface. <sup>[1]</sup></p></li>
  <li><p><code>MyClass::instanceMethod</code> that takes an argument of type <code>Outer</code> and is a <code>Function&lt;T,R&gt;</code>functional interface. <sup>[1]</sup></p></li>
  </ol>
  
  <p>is valid. Then how does the <code>map</code> function know to apply the function in option (1) to the objects of the stream, but pass the stream objects to function in option (2)? </p>
  
  <p>[1] <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p>