With Java < 8 you can create an interface (note that there already is an IntFunction
interface in Java 8):
interface IntFunction<A> { A apply (int i); }
m(elementA, a, new IntFunction<A> () { public A apply(int i) { methodA(i); } });
And your method would look like:
private void m(Collection<List<Integer>> element, int a, IntFunction<A> f) {
for(int i=0; i < a; i++){
List<Integer> list = element.get(i);
SomeClass rg = new SomeClass(list, a, f.apply(i));
int result = rg.generate();
}
}
(I have omitted the methodA2
for conciseness: you would need a second interface that has an apply(int, int)
)
That is quite verbose and the benefit is not obvious vs. repetition.
With Java 8 it becomes cleaner:
m(elementA, a, i -> methodA(i));
//or
m(elementA, a, this::methodA);