Special Edition Using Java 2, Standard Edition (Special Edition Using...)

   

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.

   

Категории