title: Java Lambdas: Sending method name as parameter
body: <p>I've run into a problem in which my class contains several methods with a lot of duplicated code. The reason behind this is that each method traverses a list of entries and calls specific entry method. </p>
  
  <p>In code...</p>
  
  <p>The LowLevelClass class has the following structure:</p>
  
  <pre><code>public class LowLevelClass {
  
      // constructor omitted
  
      public boolean doSomethingA() {
          // some non-duplicated code
          return true;
      }
  
      public boolean doSomethingB() {
          // some non-duplicated code
          return true;
      }
  
      public boolean doSomethingC() {
          // some non-duplicated code
          return true;
      }
  }
  </code></pre>
  
  <p>The top level class contains a List of LowLevelClasses and has the same number of methods, but this time, with a lot of duplications:</p>
  
  <pre><code>public class HighLevelClass {
  
      private List&lt;LowLevelClass&gt; classes = new ArrayList&lt;&gt;();
  
      public HighLevelClass() {
          this.classes.add(new LowLevelClass(/* params */));
          this.classes.add(new LowLevelClass(/* params */));
          this.classes.add(new LowLevelClass(/* params */));
      }
  
      public void doA() {
          System.out.println("Doing ...");
          for (LowLevelClass entry : classes) {
              System.out.println("Doing something...");
              entry.doSomethingA();
              System.out.println("Done");
          }
      }
  
      public void doB() {
          System.out.println("Doing ...");
          for (LowLevelClass entry : classes) {
              System.out.println("Doing something...");
              entry.doSomethingB();
              System.out.println("Done");
          }
      }
  
      public void doC() {
          System.out.println("Doing ...");
          for (LowLevelClass entry : classes) {
              System.out.println("Doing something...");
              entry.doSomethingC();
              System.out.println("Done");
          }
      }
  }
  </code></pre>
  
  <p>My goal is to have something in form of:</p>
  
  <pre><code>public class HighLevelClass {
  
      private List&lt;LowLevelClass&gt; classes = new ArrayList&lt;&gt;();
  
      public HighLevelClass() {
          this.classes.add(new LowLevelClass());
          this.classes.add(new LowLevelClass());
          this.classes.add(new LowLevelClass());
      }
  
      public void doSomething(Lambda /* Functional interface*/ operation) {
          System.out.println("Doing A");
          for (LowLevelClass entry : classes) {
              System.out.println("Doing something...");
              entry.operation; // or something else...
              System.out.println("Done");
          }
      }
  
      public void doSomethingA() {
          // my goal... and maybe in totally wrong direction is to send something in form of...
          return doSomething(LowLevelClass::doSomethingA);
      }
  
      // etc
  }
  </code></pre>
  
  <p>Can this be done in Java 8 with Lambdas? In other words, can I define the method to perform on each entry of the given list?</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>The answers provided by <strong>Jorn Vernee</strong> and <strong>Joffrey</strong> are correct! </p>
  
  <p>Ultimately, the solution was to use Predicate. (see EDIT 2 why I didn't use Consumer in the end...) </p>
  
  <p>public class HighLevelClass {</p>
  
  <pre><code>private List&lt;LowLevelClass&gt; classes = new ArrayList&lt;&gt;();
  
  public HighLevelClass() {
      this.classes.add(new LowLevelClass());
      this.classes.add(new LowLevelClass());
      this.classes.add(new LowLevelClass());
  }
  
  public boolean doSomething(Predicate&lt;LowLevelClass&gt; function) {
      System.out.println("Doing A");
      for (LowLevelClass entry : classes) {
          System.out.println("Doing something...");
          boolean val = function.test(entry);
          System.out.println("Done " + val);
      }
      return someEndVerdict; 
  }
  
  public boolean doSomethingA() {
      return doSomething(LowLevelClass::doSomethingA);
  }
  
  // etc
  </code></pre>
  
  <p>}</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>My initial methods in HighLevelClass didn't contain boolean return type. That's the reason why I used Predicate (Predicate, as a contast to Consumer, returns boolean value which suited me better - and which I forgot to initially mention :((( )</p>
  
  <p>Thanks for help and time!</p>