java.util.Collection with the lowest overhead?

I’m calling a method in another API that accepts a java.util.Collection of objects. I’ve looked at the method and it immediately copies everything in the collection into a new ArrayList before performing its task.

This got me wondering: What is the absolute lowest overhead Java Collection that I can use to quickly assemble parameters for this method?

Answer

That depends on how it copies the elements, but if it creates the ArrayList-copy like this

new ArrayList<Something>(inputCollection);

or if it does

someCopy.addAll(inputCollection);

then the it will go through the inputCollection.toArray() which is probably best implemented by ArrayList.

Leave a Reply

Your email address will not be published. Required fields are marked *