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?
The ArrayDeque class is often the best choice for managing a deque. However, in some cases, a LinkedList will be faster. If performance is important, experiment to see if LinkedList performs better in your application. LinkedList: The LinkedList class can be used to organize objects into a deque.
If you need fast access to elements using index, ArrayList should be choice. If you need fast access to elements using a key, use HashMap.
Collections class is one of the utility classes in Java Collections Framework. The java. util package contains the Collections class. Collections class is basically used with the static methods that operate on the collections or return the collection.
Three of these interfaces, Set, List, and SortedSet are descendants of the Collection interface; they add further constraints on the contracts imposed by the methods in this interface, as well as adding new methods.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With