title: How do I pass function references into my method and use them?
body: <p>I have a working method that I'd like to make more generic using lambdas or function references. That is, for some of the methods that are called within my method, I'd like to specify them as arguments to my method. However I can't quite get it.</p>
  
  <p>Let's say I have the following basic class structure</p>
  
  <pre><code>class B {}
  class C {
      public Set&lt;Integer&gt; getIds() { ... }
      public void addB(B b) { ... }
  }
  </code></pre>
  
  <p>And I have my working method</p>
  
  <pre><code>private void p1(Map&lt;Integer, C&gt; cs, Map&lt;Integer, B&gt; bs) {
      for (C c : cs.values()) {
          for (Integer id : c.getIds()) {
              if (bs.containsKey(id)) {
                  c.addB(bs.get(id));
              }
          }
      }
  }
  </code></pre>
  
  <p>Which is called thus <code>p1(cs, bs);</code> - so far so good.</p>
  
  <p>The first step in making this more generic is to provide a lambda for the <code>getIds()</code> method. I have achieved this using:</p>
  
  <pre><code>private void p1(Map&lt;Integer, C&gt; cs, Map&lt;Integer, B&gt; bs, Function&lt;C, Set&lt;Integer&gt;&gt; f) {
      for (C c : cs.values()) {
          for (Integer id : f.apply(c)) {  // Change here
              if (bs.containsKey(id)) {
                  c.addB(bs.get(id));
              }
          }
      }
  }
  </code></pre>
  
  <p>Which is called thus:  <code>p1(cs, bs, C::getIds);</code> - again, so far so good. I am now passing in <code>getIds()</code> as an argument to my function and making use of it.</p>
  
  <p>Now I want to provide a lambda for the <code>addB()</code> method as the fourth argument to <code>p1()</code> and I cannot find the right way. I have tried several things, eg. <code>Function</code> and <code>Consumer&lt;B&gt;</code>, but I cannot figure out how to do it. Any suggestions?</p>