6 minutes read

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, Queue and Set interface store singular elements, whereas the implementations of the Map interface always store key-value pairs.

  • Duplicates or uniqueness of elements: the implementations of the Set interface store only unique elements, while List's can store duplicates.

  • Access type: ArrayList provides access to its elements by index, but ArrayDeque provides 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 LinkedHashSet has worse performance than an ordinary HashSet.

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.

Decision-making diagram

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.

  1. If you can't choose an implementation using this diagram in a particular case, we recommend using the general-purpose implementations: ArrayList, HashSet and HashMap. 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).

  2. 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 List interface instead of ArrayList in all methods, fields, and local variables. If you only need to use the methods like add, remove, clear, and the information about ordering and duplicates isn't important, then prefer using the Collection interface.

  3. 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.

  4. As LinkedList implements both List and Queue interfaces, sometimes it isn't fully obvious what to choose between ArrayList and LinkedList or LinkedList and ArrayDeque. In modern versions of Java, the array-based collections are stated to be more performant and should be used in most standard cases. LinkedList can 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 that LinkedList will 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.

72 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo