Some best practices for Java collections usage

Use ArrayLists, HashMap etc vs. Vector, Hashtable etc, wherever possible to avoid any synchronization overhead. Even better is to use just arrays where possible. If multiple threads concurrently access a collection and at least one of the threads either adds or deletes an entry into the collection, then the collection must be externally synchronized. This can be achieved by:

Map sampleMap = Collections.synchronizedMap (sampleMap);

List sampleList = Collections.synchronizedList (sampleList);

Note : Collections is utility class different from Collection Inerface.

Set the initial capacity of a collection appropriately (e.g. ArrayList, HashMap etc). This is because collection classes like ArrayList, HashMap etc must grow periodically to accommodate new elements. But if you have a very large array, and you know the size in advance then you can speed things up by setting the initial size appropriately.

For Eg: HashMaps/Hashtables need to be created with sufficiently large capacity to minimize rehashing (which happens every time the table grows). HashMap has two parameters initial capacity & load factor that affect its performance and space requirements. Higher load factor values (default load factor of 0.75 provides a good trade off between performance and space) will reduce the space cost but will increase the lookup cost of sampleMap.get(…) & sampleMap.put(…) methods. When the number of entries in the HashMap exceeds the current capacity * loadfactor then the capacity of the HasMap is roughly doubled by calling the rehash function. It is also very important not to set the initial capacity too high or load factor too low if iteration performance or reduction in space is important.

Program in terms of interface not implementation: For eg you might decide a LinkedList is the best choice for some application, but then later decide ArrayList might be a better choice for performance reason.

Please use:

List sampleList = new ArrayList(100); //program in terms of interface & set the initial size.

In the place of (avoid this):

ArrayList sampleArrlist = new ArrayList();

Avoid storing unrelated or different types of objects into same collection: This is analogous to storing items in pigeonholes without any labeling. To store items use value objects or data objects (as oppose to storing every attribute in an ArrayList or HashMap). Provide wrapper classes around your collection API classes like ArrayList, Hashmap etc as shown in better approach column. Also where ever applicable consider using composite design pattern, where an object may represent a single object or a collection of objects.

This brings us to discuss to the details of composite design pattern…I will take it up some time later for now lets keep koding…:-)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s