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 inserts and removals is more important than that of locating specific elements.

  • Use TreeMap when you need to iterate over a set of key-value pairs in sorted key order.

  • Use TreeSet when you need to maintain a set of objects in sorted order.

  • Use the synchronized wrappers provided by the Collections class if thread safety is an issue; avoid their overhead if it isn't.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net