Java collection Best Practices?
by, 01-08-2012 at 11:22 AM (1805 Views)
Following are the best practices related to collection framework.
• Use ArrayLists, HashMap when required to avoid thread synchronization overhead. Also you can use arrays when possible. You can achieve thread synchronization by:
• Specify the initial values for ArrayList and HashMap because these grow periodically to get space for new elements.Java Code: This is an example of list and mapMap myMap = Collections.synchronizedMap(myMap); List myList = Collections.synchronizedList(myList);
• Write your code using interfaces not implementation.
• Do not store unrelated objects into a collection. Write wrapper classes for your collection classes. Also try to use composite design pattern.