Which Collection Class to Use?

Given the number of collection classes, the difficulty is sometimes in deciding which one to use for a particular need. The following guidelines outline some recommendations you should consider when making this choice:

  • Use the general-purpose implementations in the collections framework instead of Vector, Stack, and Hashtable, unless you are restricted to a JDK 1.1.x version.

  • Use ArrayList for ordered collections (instead of Vector).

  • Use HashSet for unordered collections.

  • Use HashMap for key-value associations (instead of Hashtable).

  • Use ArrayList when random access of the elements is more important than the time required to perform inserts and removals in the middle of the list.

  • Use LinkedList when the performance of ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.