Java: Choosing the right Collection

Here is a quick guide for selecting the proper implementation of a Set , List , or Map in your application.

The best general purpose or ‘primary’ implementations are likely ArrayList, LinkedHashMap, and LinkedHashSet. Their overall performance is better, and you should use them unless you need a special feature provided by another implementation. That special feature is usually ordering or sorting.

Here, ‘ordering’ refers to the order of items returned by an Iterator, and ‘sorting’ refers to sorting items according to Comparable or Comparator.

InterfaceHasDuplicates?ImplementationsHistorical
SetnoHashSetLinkedHashSet*TreeSet
ListyesArrayList*LinkedListVectorStack
Mapno duplicate keys HashMapLinkedHashMap*TreeMapHashtable,Properties

Principal features of non-primary implementations :

  • HashMap has slightly better performance than LinkedHashMap
  • HashSet has slightly better performance than LinkedHashSet
  • TreeSet is ordered and sorted, but slow
  • TreeMap is ordered and sorted, but slow
  • LinkedList has fast adding to the start of the list, and fast deletion from the interior via iteration

Iteration order for above implementations :

  • HashSet – undefined
  • HashMap – undefined
  • LinkedHashSet – insertion order
  • LinkedHashMap – insertion order of keys (by default), or ‘access order’
  • ArrayList – insertion order
  • LinkedList – insertion order
  • TreeSet – ascending order, according to Comparable / Comparator
  • TreeMap – ascending order of keys, according to Comparable / Comparator

For LinkedHashSet and LinkedHashMap, the re-insertion of an item does not affect insertion order.

While being used in a Map or Set, these items must not change state (hence, it is recommended that these items be immutable objects):

  • keys of a Map
  • items in a Set

Sorting requires either that :

To retain the order of a ResultSet as specified in an ORDER BY clause, insert the records into a List or a LinkedHashMap.

Reference: Choosing the right Collection from our JCG partner Sanjeev Kumar at the Architect’s Diary blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


5 + = twelve



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close