Sunday, October 23, 2011

Collections in Java

First keep in mind that the elements of collections in Java should be reference type only, not primitive type.

There are four types of collections and each type includes several classes.
TypeClasses
ListArrayList, LinkedList, CopyOnWriteArrayList
SetHashSet, TreeSet, LinkedHashSet, CopyOnWriteArraySet, ConcurrentSkipListSet
MapHashMap, TreeMap, LinkedHashMap, ConcurrentHashMap, ConcurrentSkipListMap
QueueLinkedList, PriorityQueue, SynchronousQueue, DelayQueue, ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue, ConcurrentLinkedQueue

Basic approach to choosing a collection:
  1. Select the collection type;
  2. Select specific class of that type, the one with extra functionality as few as possible.

Step 1: Select collection type
TypeFunctionalityTypical uses
List
  • Essentially a variable-size array;
  • You can usually add/remove items at any arbitrary position;
  • The order of the items is well defined (i.e. you can say what position a given item goes in in the list).
Most cases where you just need to store or iterate through a "bunch of things" and later iterate through them.
Set
  • Things can be "there or not"— when you add items to a set, there's no notion of how many times the item was added, and usually no notion of ordering.
  • Remembering "which items you've already processed", e.g. when doing a web crawl;
  • Making other yes-no decisions about an item, e.g. "is the item a word of English", "is the item in the database?" , "is the item in this category?" etc.
Map
  • Stores an association or mapping between "keys" and "values"
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:
  • For a given user ID, what is their cached name/User object?
  • For a given IP address, what is the cached country code?
  • For a given string, how many instances have I seen?
Queue
  • Like a list, but where you only ever access the ends of the list (typically, you add to one end and remove from the other).
  • Often used in managing tasks performed by different threads in an application (e.g. one thread receives incomming connections and puts them on a queue; other "worker" threads take connections off the queue for processing);
  • For traversing hierarchical structures such as a filing system, or in general where you need to remember "what data to process next", whilst also adding to that list of data;
  • Related to the previous point, queues crop up in various algorithms, e.g. build the encoding tree for Huffman compression.


Step 2: Select specific class

List
ClassFeatures/implementationWhen to use
ArrayList
  • Allows elements to be efficiently read by index.
  • Adding/removing the last element is efficient.
  • Not synchronized in any way.
In most cases.
LinkedList
  • First and last elements can be accessed efficiently;
  • Other elements cannot be efficiently accessed by index;
  • Not synchronized in any way.
Effectively, functions as a non-synchronized queue. In practice, rarely used: when you need a queue, you often need it to be concurrent or to provide other functionality; other implementations are often more useful.
CopyOnWriteArrayList
  • Allows safe concurrent access;
  • Reads are efficient and non-blocking;
  • Modifications are not efficient (since a brand new copy of the list is taken each time).
Where you need concurrent access and where frequency of reads far outweights frequency of modifications.

Set
Ordering of keysNon-concurrentConcurrent
No particular orderHashSet
SortedTreeSetConcurrentSkipListSet
FixedLinkedHashSetCopyOnWriteArraySet

Map
Ordering of keysNon-concurrentConcurrent
No particular orderHashMapConcurrentHashMap
SortedTreeMapConcurrentSkipListMap
FixedLinkedHashMap

Queue
Blocking?Other criteriaBoundNon-bound
BlockingNoneArrayBlockingQueueLinkedBlockingQueue
Priority-based PriorityBlockingQueue
Delayed DelayQueue
Non-blockingThread-safe ConcurrentLinkedQueue
Non thread-safe LinkedList
Non thread-safe, priority-based PriorityQueue


Reference:
http://www.javamex.com/tutorials/collections/how_to_choose.shtml

No comments:

Post a Comment