Posts

Showing posts from January, 2010

Which one is Better Vector or SynchronizedList

Vector and SynchronizedList are synchronized. In case of vector the synchronization is at method level, Example: public synchronized E get(int index) { return (E)elementData[index]; } And in case of SynchronizedList, the statements within the methods are synchronized on the list object. SynchronizedList is static inner class inside collections class which implements list interface and necessary methods using synchronized blocks like below. Link to Source code Example : public E get(int index) { synchronized(colobj) {return list.get(index);} } where colobj is the collection object. When you do Collections.synchronizedList(new ArrayList()); you end up creating 2 instances of List. Whereas, in case of Vector, you are creating only one instance.

Choosing right collection

                The performance of code can be affected significantly by choosing the right collection implementation. Regarding the correct and efficient use of the Java Collection classes, a significant performance setback comes from the low utilization of implementation classes other than ArrayList and HashMap. These two implementations are used for almost everything, although different implementations would be more appropriate in some situations. The following rules should be applied to select the appropriate collection class: ·         Use ArrayList to collect data in order of occurrence (append operation only). Adding or removing elements at the bottom of the list is very fast on ArrayLists and LinkedList, but the LinkedList puts a high load on the garbage collector. This is the most common use case for lists. ·         Use LinkedList to maintain lists whenever elements have to be inserted or removed at positions other than the the bottom of the list. Inserting or remov