title: Working with an ArrayList of Functions in Java-8
body: <p><strong>Problem Description:</strong>
  I want to be able to use an ArrayList of Functions passed in from another class  (where the Functions have been defined in that other class). If the list of Functions, which may have different input and return types, are defined in one class, I want to be able to pass an ArrayList of some of them, with possible duplicates, as a parameter to some other class's constructor or method and perform operations using them. </p>
  
  <p><strong>Code Description:</strong>
  The code below is a greatly simplified example which is not intended to make much sense from a design perspective. The focus of the problem is the method <code>getResult()</code> within <code>SomeClass</code> and generally how to use an ArrayList of Functions once you have them.</p>
  
  <p><strong>Attempt to solve the problem:</strong>
  The getResult() method implementation is an example of one of many attempts to use the Function list. Again, please don't mind the design of the code. It was just done that way to try to make the problem example as short as possible.</p>
  
  <p>Simple tester class</p>
  
  <pre><code>package com.Testing;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Random;
  import java.util.function.Function;
  
  public class Tester {
  
      public static void main(String[] args)
      {
          // Some functions
          Function&lt;Integer, Integer&gt; increment = (Integer input) -&gt; {
              return input + 1;
          };
  
          Function&lt;Integer, Integer&gt; decrement = (Integer input) -&gt; {
              return input - 1;
          };
  
          Function&lt;Double, Double&gt; timesPi = (Double input) -&gt; {
              return input * 3.14;
          };
  
          // list of Functions
          List&lt;Function&gt; availableMathOperations = new ArrayList&lt;&gt;();
          List&lt;Function&gt; selectedMathOperations = new ArrayList&lt;&gt;();
  
          // populate master list
          availableMathOperations.add(increment);
          availableMathOperations.add(decrement);
          availableMathOperations.add(timesPi);
  
          // Populate random selection list //
          //    generate random binary number
          Random randomGenerator = new Random();
          int randomNumber = randomGenerator.nextInt(availableMathOperations.size() * 2);
          boolean[] bits = new boolean[availableMathOperations.size()];
          for (int j = 0; j &lt; availableMathOperations.size(); j++) {
              bits[availableMathOperations.size() - 1 - j] = (1 &lt;&lt; j &amp; randomNumber) != 0;
          }
  
          // add math operations to selectedMathOperations based on binary number
          for (int j = 0; j &lt; bits.length; j++) {
              if (bits[j]){
                  selectedMathOperations.add(availableMathOperations.get(j));
              }
          }
  
          SomeClass someClass = new SomeClass(selectedMathOperations, 1.23);
  
      }
  }
  </code></pre>
  
  <p>Other class </p>
  
  <pre><code>package com.Testing;
  
  import java.util.List;
  import java.util.function.Function;
  
  public class SomeClass {
  
      List&lt;Function&gt; operations;
      double initialValue;
  
      public SomeClass(List&lt;Function&gt; newOperations, double newInitialValue){
          operations = newOperations;
          initialValue = newInitialValue;
      }
  
      public double getResult(){
          double result = 0.0;
  
          // problem method
          // perform the random list of operations using the initial value initially
          for(int i = 0; i &lt; operations.size(); i++){
              if(i == 0)
                  result = operations.get(i)(initialValue);
              else
                  result += operations.get(i)(result);
          }
          return result;
      }
  
  }
  </code></pre>