title: Java pass method reference as parameter to other method
body: <p>I am trying to pass a selected "get"-method of class A to a method in class B. I have checked out <a href="https://stackoverflow.com/questions/2186931/java-pass-method-as-parameter">Java Pass Method as Parameter</a>, but I was not able to adopt the interface approach to my problem in a reasonable way. I would prefer to <strong>not</strong> use java 8 (lambdas) and if possible avoid reflection as well. My feeling is, that I am looking at my problem the wrong way.
  Here is the specific simplified example of what I am trying to accomplish:</p>
  
  <p>I have a class containing some fields and get-methods:</p>
  
  <pre><code>public class DataStore {
      private float a;
      private float b;
      private float c;
  
      public float getA() {
          return a;
      }
  
      public float getB() {
          return b;
      }
  
      public float getC() {
          return c;
      }
  
  }
  </code></pre>
  
  <p>Next I have my main class instantiating <code>DataStore</code> as Values of a Map and then accessing specific fields of DataStore like:</p>
  
  <pre><code>public class App {
  
      public static void main(String[] args) {
          // declare TreeMap using DataStore class as value
          Map&lt;Integer, DataStore&gt; dataMap = new TreeMap&lt;Integer, DataStore&gt;();
  
          // populate Map with example data
          dataMap.put(2,  new DataStore(1f,2f,3f));
          dataMap.put(10, new DataStore(3f,4f,5f));
          dataMap.put(4,  new DataStore(6f,7f,8f));
  
          // work with specific fields in DataStore, e.g. assign to array
          float[] aArray = getValuesAsArray(dataMap, DataStore.getA());
          float[] bArray = getValuesAsArray(dataMap, DataStore.getB());
          float[] cArray = getValuesAsArray(dataMap, DataStore.getC());
      }
  
      /**
       * Assign specific field of DataStore from Map to Array
       * @param dataMap
       * @param getVar - reference for specified getter method
       * @return 
       */
      private static float[] getValuesAsArray(Map&lt;Integer, DataStore&gt; dataMap, MethodReference getVar()) {
          int i = 0;
          int nMap = dataMap.size();
          float[] fArray = new float[nMap];
          for (Map.Entry&lt;Integer, DataStore&gt; entry : dataMap.entrySet()) {
              DataStore ds = entry.getValue();
              fArray[i] = ds.getVar();
              i++;
          }
          return fArray;
      }
  }
  </code></pre>
  
  <p>Clearly this wont work, as I have to figure out how to pass my selected get method into <code>getValuesAsArray()</code>.
  Somehow, I guess, my approach may be wrong. So I am open for suggestions. </p>