Saturday, June 13, 2020

What are the some of the best practices related to collections ?


While using Collections in Java programming the java developer needs to understand and implement the best practices to improve the code quality and improve the performance of the application. That will enhance the stability , performance and maintainability of the java applications.

  • Use ArrayLists , Hashmaps etc instead of Vector and Hashtable etc, where possible to avoid any synchronization overhead. Even better is to use Arrays where possible if you know the size in advance. 
  • 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 instead of using default synchronized classes like Vector and Hashtable. This can be achieved by using the below piece of code :
                Map myMap = Collections.synchronizedMap(myMap); 

                List myList = Collections.synchronizedList(myList);

  • Set the initial capacity 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 up the initial size appropriately.
For Example: 
  • 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 and load factor that affect its performance and space requirements. 
  • Heigher load factor values (default load factor of 0.75 provides a good trade off between preformance and space) will reduce the space cost but will increase the lookup cost myMap.get(...) and myMap.put(...) methods. When the number of entries in the HashMap exceeds the current capacity * loadfactor then the capacity of the HashMap is roughly doubled by calling the rehash function. It is also very important not to set the initial capacity too high ot load factor too low if iteration performance or reduction in space is imporatant.
  • Program in terms of Interface not implementation: For example you might decide a LinkedList is the best choice for some application, but then later came to know that the ArrayList might be a better choice for better performance reason. In that scenario you need to code as below :
Code : List list = new ArrayList(100); //Application in terms of interface & set the initial capacity.

instead of as below :
 ArrayList list = new ArrayList();

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

No comments:

Post a Comment

Hello Buddy, if you have any doubts or need any clarification , feel free to comment. Thanks.