I'm getting a List
of object A, then I use Apache Commons Collection4
to transform the obtained List
from having A instances to having B instances.
listOfBs = (List<B>) CollectionUtils.collect(listOfAs, componentTransformer);
However, eventually I need to have an Array
of Bs not a List
.
So my question is, which is faster.
- Convert the list using
CollectionUtils.collect
- Create an array using
listOfBs.toArray(new B[listOfBs.size()])
Or
- Loop over the
listOfAs
- Transform each A object to a B object
- Add each B object to an array (B[])
The difference between the first approach and the second approach is that the first approach has much less code, but I'm not sure if the toArray method bares a hidden loop or expensive operations.
What I see in the second approach is that I'm sure I'll loop only once over the listOfAs
list.
So which approach is faster ?
CollectionUtils
. Even better, just use Java 8. – HosierytoArray
doesn't cause a big performance hit, I may use it. – PhionnaCollectionUtils.collect
it is looping through list and transforming object, which is your first two steps from your alternative method – Colvin