Java collections are one of the most commonly used data-structures by all Java professionals. But are you using the right collection class that would best suits your need. Most programmers usually end up useing Vectors, ArrayList, HashMap or Hashtable. There are many other collection classes available with the JDK that you can use instead of re-inventing some common logic that might suite your need.
Factors that could help on deciding a Collection
There are various factors that can be considered when selecting an appropriate collection for a particular problem. These factors are:
- Ordering - Some sort of ordering in the elements. For example, sorted order, insertion order or no specific ordering.
- Duplicates - May or may not want to allow duplicate elements in a collection.
- Thread Safe - Ensure the safety of the elements in a collections in case there are multiple threads accessing it.
- Key-Value pair - Store in key-value pairs.
- Blocking operations - Wait for the collection to become non-empty when retrieving an element.
- Random Access - Instant retrieval of an element.
- Upper Bounds - To limit the maximum number of elements a collection can hold.
There are also other factors like priority, delay etc..