18 October 2009

Selecting the Best Java Collection Class for Your Application

Selecting the Best Java Collection Class for Your Application - referenced from http://www.javaperformancetuning.com/news/newtips104.shtml
* With CopyOnWriteArrayList all operations that change the contents of a CopyOnWriteArrayList collection cause the underlying array to be replaced with a copy of itself before the contents of the array are changed. Any active iterators will continue to see the unmodified array, so there is no need for locks.
* HashSet is faster than TreeSet, so only use the TreeSet preferentially when you need elements to remain in sorted order.
* HashSet is faster than LinkedHashSet so only use the LinkedHashSet preferentially when you need elements to remain in insertion order.
* ConcurrentSkipListSet keeps it's elements in sorted order and is thread-safe and usually preferable to a synchronized wrapped set.
* With CopyOnWriteArraySet all operations that change the contents of a CopyOnWriteArraySet collection cause the underlying array to be replaced with a copy of itself before the contents of the array are changed. Any active iterators will continue to see the unmodified array, so there is no need for locks.
* PriorityQueue add and remove methods take time that is proportionate to the number of objects in the queue. Queues based on the PriorityQueue class do not block. PriorityBlockingQueue is similar in performance but does block.
* For situations when multiple threads are waiting to perform operations on a queue, the behavior of an ArrayBlockingQueue object will be more consistent and predictable than for a LinkedBlockingQueue.
* ConcurrentLinkedQueue is thread-safe and usually preferable to a synchronized wrapped queue.
* DelayQueue is a specialized blocking queue that consults the objects it contains as to when they can be removed from the queue.
* A SynchronousQueue object is always empty; the put method blocks unless another thread is waiting for the object's take method to return.
* If you are not interested in how objects will be organized in a collection, then the only other consideration is performance. In that case, use the ArrayList class. It is fast and makes efficient use of memory.

No comments:

Post a Comment