Monday, August 31, 2009

Some Java String & Collections API notes

Some summary of String and Collections API usage to remind myself :)

Criteria to choose among String, StringBuffer and StringBuilder:
1. If your text is not going to change use a string Class because a String object is immutable.

2. If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.

3. If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.


Collections API:
1. Vectors and Hashtable classes are available from the initial JDK 1.0. But, ArrayList and HashMap are added as a part of new Collections API since JDK 1.2.

2. Vectors and Hashtable are synchronized where as ArrayList and HashMap are unsynchronized.

3. Use Vector if there are multiple threads and ArrayList if there is only a single thread.

4. Use Hashtable if there are multiple threads and HashMap if there is only a single thread.

No comments: