I'm working with Java 7, and I'm searching in the Guava API for a way to apply a function to an array without having to convert it to a Collection first. I'm willing to create my own class for such purpose, but I don't want to reinvent the wheel hehe.
So as a summary (in case you don't know exactly what I'm talking about), this is what I've found so far that you can do with Guava in order to apply a function to an array as I said:
Integer[] someNumbers = new Integer[]{1, 2, 3};
Integer[] returnedNumbers = Collections2.transform(Arrays.asList(someNumbers), squareNumberFunction).toArray(new Integer[0]);
assertThat(returnedNumbers).isEqualTo(new Integer[]{1, 4, 9});//Using AssertJ here
But I would like to be able to do something like this instead:
Integer[] someNumbers = new Integer[]{1, 2, 3};
Integer[] returnedNumbers = Arrays.transform(someNumbers, squareNumberFunction);
assertThat(returnedNumbers).isEqualTo(new Integer[]{1, 4, 9});
Ideally the functionality I'm talking about would be type-safe.
EDIT
For even further clarification of the problem:
- The arrays I'm talking about are not primitive arrays, they reference complex objects (I only used integers to easily exemplify what I was talking about).
- I have no control over the received or send structures, they are arrays (imagine a legacy code situation where that's possible if you think that helps you understand the problem better).
- Efficiency is a must when transforming the arrays and accessing them.