Your getX() methods can be thought of as a function that takes an instance of a DataStore and returns a float.
In Java 8, you can represent them using method references:
float[] aArray = getValuesAsArray(dataMap, DataStore::getA); float[] bArray = getValuesAsArray(dataMap, DataStore::getB); float[] cArray = getValuesAsArray(dataMap, DataStore::getC);
Then your getValuesAsArray will take a Function<DataStore,Float> parameter and execute the function:
private static float[] getValuesAsArray(Map<Integer, DataStore> dataMap, Function<DataStore,Float> func) { int i = 0; int nMap = dataMap.size(); float[] fArray = new float[nMap]; for (Map.Entry<Integer, DataStore> entry : dataMap.entrySet()) { DataStore ds = entry.getValue(); fArray[i] = func.apply(ds); i++; } return fArray; }
Without using Java 8, you can define your own interface containing a method that takes an instance of a DataStore and returns a float . Then, instead of using Java 8 method getValuesAsArray you need to pass your getValuesAsArray method getValuesAsArray implementation of this interface (you can use an anonymous class instance that implements the interface) that calls one of the getX() methods.
For instance:
public interface ValueGetter { public float get (DataStore source); } float[] aArray = getValuesAsArray(dataMap, new ValueGetter() {public float get (DataStore source) {return source.getA();}}); float[] bArray = getValuesAsArray(dataMap, new ValueGetter() {public float get (DataStore source) {return source.getB();}}); float[] cArray = getValuesAsArray(dataMap, new ValueGetter() {public float get (DataStore source) {return source.getC();}});
and
private static float[] getValuesAsArray(Map<Integer, DataStore> dataMap, ValueGetter func) { int i = 0; int nMap = dataMap.size(); float[] fArray = new float[nMap]; for (Map.Entry<Integer, DataStore> entry : dataMap.entrySet()) { DataStore ds = entry.getValue(); fArray[i] = func.get(ds); i++; } return fArray; }