What is the main difference between an ArrayList and a Vector? What is the main difference between HashMap and Hashtable?

Vector / Hashtable ArrayList / HashMap
Original classes before the introduction of Collections API. Vector & Hashtable are synchronized. Any method that touches their contents is thread-safe. So if you don’t need a thread safe collection, use the ArrayList or HashMap. Why pay the price of synchronization unnecessarily at the expense of performance degradation.

As a general rule, prefer ArrayList/HashMap to Vector/Hashtable. If your application is a multithreaded application and at least one of the threads either adds or deletes an entry into the collection then use new Java collections API‘s external synchronization facility as shown below to temporarily synchronize your collections as needed:

Map myMap = Collections.synchronizedMap (myMap); // single lock for the entire map
List myList = Collections.synchronizedList (myList); // single lock for the entire list

If you are using J2SE5, you should use the new “java.util.concurrent” package for improved performance because the concurrent package collections are not governed by a single synchronized lock as shown above. The “java.util.concurrent” package collections like ConcurrentHashMap is threadsafe and at the same time safely permits any number of concurrent reads as well as tunable number of concurrent writes. The “java.util.concurrent” package also provides an efficient scalable thread-safe non-blocking FIFO queue like ConcurrentLinkedQueue.
The “java.util.concurrent” package also has classes like CopyOnWriteArrayList, CopyOnWrite-ArraySet, which gives you thread safety with the added benefit of immutability to deal with data that changes infrequently. The CopyOnWriteArrayList behaves much like the ArrayList class, except that when the list is modified, instead of modifying the underlying array, a new array is created and the old array is discarded. This means that when a caller gets an iterator (i.e. copyOnWriteArrayListRef.iterator() ), which internally holds a reference to the underlying CopyOnWriteArrayList object’s array, which is immutable and therefore can be used for traversal without requiring either synchronization on the list copyOnWriteArrayListRef or need to clone() the copyOnWriteArrayListRef list before traversal (i.e. there is no risk of concurrent modification) and also offers better performance.

Array List / Stack etc
Java arrays are even faster than using an ArrayList/Vector and perhaps therefore may be preferable if you know the size of your array upfront (because arrays cannot grow as Lists do). ArrayList/Vector are specialized data structures that internally uses an array with some convenient methods like add(..), remove(…) etc so that they can grow and shrink from their initial size. ArrayList also supports index based searches with indexOf(Object obj) and lastIndexOf(Object obj) methods.
In an array, any item can be accessed. These are more abstract than arrays and access is restricted. For example, a stack allows access to only last item inserted.
Tagged , . Bookmark the permalink.

Leave a Reply