The Java Collections API provides a whole host of data structures, especially since the API was expanded in Java 5 (and again slightly in Java 6) to include concurrent collections. At first, the array of choices can be a little daunting: should I use a HashMap or a LinkedHashMap? When should I use a list or a HashSet? When should I use a TreeMap rather than a HashMap? But with a bit of guidance, the choice needn't be quite so daunting. There are also a few cases where it's difficult to decide because the choice is very arguable. And in other cases, having a clear set of rules of thumb can guide you to an appropriate decision.
The overall approach I'd suggest for choosing is as follows:
In general, the algorithm that underlies each collection class is designed to be a good tradeoff between efficiency and certain minimal requirements. So as long as you understand the minimal requirements that a given class is designed to provide, you shouldn't need to get too bogged down in the actual algorithms (though if you are interested in algorithms, the source code to all the collections classes is available and they make fascinating case studies, of course).
The first part of the decision is choosing what "basic category" of organisation or functionality your data needs to have. The broad types are as follows:
Collection type | Functionality | Typical uses |
---|---|---|
List |
|
Most cases where you just need to store or iterate through a "bunch of things" and later iterate through them. |
Set |
|
|
Map |
|
Used in cases where you need to say "for a given X, what is the Y"? It is often useful
for implementing in-memory caches or indexes. For example:
|
Queue |
|
|
The first three of these are really "bread and butter" collection types. On the other hand, it's probably fair to say that queues will be used less often by many programmers. Even if you are writing a multithreaded server or application involving multi-threaded job processing, you can often use the Java 5 Executor framework (which uses queues underlyingly) without needing to manipulate queues directly.
To add to the confusion, there's actually a slight overlap between lists and queues (what we often refer to as a linked list is in effect a type of queue). But for now, we'll go with the categories above and come back to the issue on the next page.
Once you've decided on the basic collection type that is most appropriate for your use, you need to select an appropriate class that implements a collection of that type. On the next page, we look specifically at how to choose which collection class to use of a given type.
If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants. Follow @BitterCoffey
Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.