Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 716
Andy Avatar asked Feb 15 '11 14:02

Andy


People also ask

Which collection is best for performance Java?

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.

Which collection is fast in Java?

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.

Is collections in Java Util?

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.

What basic interfaces are inherited from Java Util 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.


1 Answers

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.

like image 97
aioobe Avatar answered Oct 06 '22 19:10

aioobe