As you already know, the Java Collections Framework includes numerous implementations of collections such as ArrayList, LinkedList, HashSet, TreeSet, HashMap and others. Depending on the chosen implementation, your program will have different time and memory efficiency. In this regard, it is crucial to choose the right collection for the particular problem you're going to solve.
In this topic, you won't be learning the collections again. Instead, it is time for you to understand how to choose the right one.
Things to consider when choosing a collection
You have to take into account the properties of different collections to choose the one that'll do the best job. Let's briefly remember them:
Type of elements: various implementations of the
List,QueueandSetinterface store singular elements, whereas the implementations of theMapinterface always store key-value pairs.Duplicates or uniqueness of elements: the implementations of the
Setinterface store only unique elements, whileList's can store duplicates.Access type:
ArrayListprovides access to its elements by index, butArrayDequeprovides restricted access to the first and last element.Performance: usually, the more featured collection you use, the worse performance you have. For example, the performance of
LinkedHashSethas worse performance than an ordinaryHashSet.
Decision-making diagram
Here is a diagram that shows how to select a proper implementation of the Set, List, Queue or Map interfaces. Although the diagram doesn't provide an answer for 100% of cases, it still works for most of the real situations. We recommend you scrutinize it, and after a while, you will be able to choose a right collection on your own.
For easier viewing the picture below, save it locally or open it via the direct link.
Please note that these implementations are only suitable for working in a single-threaded environment. If you need a multi-threaded access, this diagram won't work for you.
Additional recommendations
There are also some points to keep in mind when consulting this diagram.
If you can't choose an implementation using this diagram in a particular case, we recommend using the general-purpose implementations:
ArrayList,HashSetandHashMap. Their overall performance is better and they should suffice in most cases. Use them unless you need a special feature provided by another implementation (e.g. ordering or sorting).Use the most abstract interface that conforms to your case so that the collection type would be easier to change in the future. For example, if you need a specific list operation, use the
Listinterface instead ofArrayListin all methods, fields, and local variables. If you only need to use the methods likeadd,remove,clear, and the information about ordering and duplicates isn't important, then prefer using theCollectioninterface.If none of the implementations provides a feature you need, then you may need to use several collections at once, or use an external collection library instead of the standard one, or create a new one.
As
LinkedListimplements bothListandQueueinterfaces, sometimes it isn't fully obvious what to choose betweenArrayListandLinkedListorLinkedListandArrayDeque. In modern versions of Java, the array-based collections are stated to be more performant and should be used in most standard cases.LinkedListcan be chosen when you have a colossal number of elements, and a lot of addition/deletion operations, especially to the beginning and the end of the list. However, there is still no guarantee thatLinkedListwill act better than others.
Conclusion
To choose the proper collection, think about what you need to store, how you want to access elements, and what is optimal in regards to performance. In the beginning, it's a good idea to stick to an algorithm we've discussed. If the algorithm doesn't help you in a particular case, there're also some general recommendations to remember. After some practice, you will be able to choose a collection quickly and accurately.