Which Java Collection Should I Use

Which Java Collection should I use?

Since I couldn't find a similar flowchart I decided to make one myself.

This flow chart does not try and cover things like synchronized access, thread safety etc or the legacy collections, but it does cover the 3 standard Sets, 3 standard Maps and 2 standard Lists.

Sample Image

This image was created for this answer and is licensed under a Creative Commons Attribution 4.0 International License. The simplest attribution is by linking to either this question or this answer.

Other resources

Probably the most useful other reference is the following page from the oracle documentation which describes each Collection.

HashSet vs TreeSet

There is a detailed discussion of when to use HashSet or TreeSet here:
Hashset vs Treeset

ArrayList vs LinkedList

Detailed discussion: When to use LinkedList over ArrayList?

Java Collections: which collection to use and when?

This should give you a pretty good breakdown...

Which java collection shall I use to store my data?

You might want to use Map collection which is useful to store key value pairs where Id is your key and value is your name.

Map<Long,String> map = new HashMap<Long,String>();

Performing the fastest search - which collection should i use?

The thing which is often skipped when comparing ArrayList and LinkedList is cache and memory management optimisations. ArrayList is effectively just an array which means that it is stored in a continuous space in the memory. This allows the Operating System to use optimisations such as "when a byte in memory was accessed, most likely the next byte will be accessed soon". Because of this, ArrayList is faster than LinkedList in all but one case: when inserting/deleting the element at the beginning of the list (because all elements in the array have to be shifted). Adding/deleting at the end or in the middle, iterating over, accessing the element are all faster in case of ArrayList.

If you need to search for student with given name and id, it sounds to me like a map with composite key - Map<Student, StudentData>. I would recommend to use HashMap implementation, unless you need to be able to both search the collection and retrieve all elements sorted by key in which case TreeMap may be a better idea. Although remember that HashMap has O(1) access time, while TreeMap has O(logn) access time.

What kind of collection should I use in this case?

It's massively more expensive to check if a List contains the element already.

Iterating over a Set is a little slower than iterating over a List but not massively so, and only by a constant factor, whereas checking for containment of an element in a List costs linear time per element and makes the whole thing quadratic.



Related Topics



Leave a reply



Submit