collections

Q 15: What is the main difference between an ArrayList and a Vector? What is the main difference between HashMap
and Hashtable? What is the difference between a stack and a queue? LF DC PI CI FAQ
A 15:
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.
Q. So which is better? 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: CO
Map myMap = Collections.synchronizedMap (myMap); // single lock for the entire map
List myList = Collections.synchronizedList (myList); // single lock for the entire list

Advertisements

Author: lwpro2

Java J2EE professional

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s