Tuesday, January 6, 2015

Performance Improvements StringBuilder and ArrayList and HashMap

Use StringBuilder if Synchronization is not required.

I have used StringBuffer when performing concatenations on string and is far better than using Immutable Strings in Java but if you don't need the synchronization, which most likely could be the case so use StringBuilder

Quote from StringBuilder API:
This class [StringBuilder] provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

Using ArrayList vs LinkedList

LinkedList is great when you don't know the size of the list as it helps.

The default initial capacity of an ArrayList is pretty small (10 from Java 1.4 - 1.7).
But since the underlying implementation is an array, the array must be re-sized if you add a lot of elements.
To avoid the high cost of resizing when you know you're going to add a lot of elements, construct the ArrayList with a higher initial capacity. What's important is to initialize the list when you know upfront the size of the ArrayList and if it is going to be more than 10

List catalogDataVOList = new ArrayList<CatalogDataVO>((int) resultCount);

Reference: http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist

Use HashMap vs Hashtable:
If Synchronization is not required use HashMap, Hashtable is a syncrhonized version and typically non synchronized objects perform better.


  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

  2. The payday advance organizations guarantee that capable borrowers just utilize their administrations in crises - as opposed to utilizing them to cover regular costs, they say, individuals come to them when a startling issue comes up, for example, unanticipated auto repairs or a high quarterly bill.

  3. So it is prudent to pick your bank carefully, by working out the net payable enthusiasm utilizing the online mini-computers for the most part gave by the loan specialists site.

  4. Know about every one of the terms and conditions that may apply. The web makes correlation shopping simple; ensure you exploit this to spare a couple of dollars on charges every payday.

  5. Startling costs manifest for everybody, and in spite of watchful budgetary administration a sudden auto repair charge or a very late chance to book a shabby occasion can scupper even the most reasonable of client's monetary plans.

  6. Savage moneylenders are promptly accessible so take the time and contact no less than 3 unique organizations preceding being in a defenseless state.

  7. Not long noticed concerning your web site and are still already reading along. I assumed i’ll leave my initial comment. i do not verify what saying except that I’ve enjoyed reading. Nice blog. ill be bookmarking keep visiting this web site really usually. https://royalcbd.com/product/cbd-gummies-10mg/