Issue
I know that some language such as python can do this:
maps = []
cur = 1
maps.append(function)
for func in self.maps:
cur = func(cur)
It adds a function to list and can iteratively call it. I'm wondering if Java can do the similar thing, if yes, how can it be done?
Solution
Yes. You can store a lambda in a list.
List<Function<Double, Double>> list = new ArrayList<>();
list.add(v->v*20);
list.add(v->v*30);
System.out.println(list.get(0).apply(10.));
System.out.println(list.get(1).apply(10.));
prints
200
300
Answered By - WJS
Answer Checked By - David Goodson (JavaFixing Volunteer)