I was just starting to learn Java Runnable , and I heard about Callable s. However, I am very struggling with this problem. I would like to make a method that takes a function as an argument (be it Callable , a Runnable or something else, if I can just call the function like coolNewFunction(() -> otherFunction(), 100) or some other simple way) and the method will return an array of otherFunction return values. For example, let's say I defined a function
public static int square(int x){ return x * x; }
Then I could do something line by line:
coolNewFunction(() -> square(), 100)
And this will return an array of the first 100 numbers and their squares (ie {{1, 1}, {2, 4}, {3, 9}...} ). Now, from the very beginning, I know that lambda () -> square() will not work, because square must be passed a value. I want to create an array of 100 Runnable , each of which has the following argument for square , but the run() method does not return anything. So, a long story, what would a method look like that evaluates another function that is given as an argument of type square for different values ββof x and returns an array of this estimate? In addition, it is advisable that I do not want to start any new topics, although if this is the only way to achieve this, but this is normal. Finally, I do not want to use the special function square (or another) (preferred).
source share