This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collection" Watch "Collection" New topic


raj joe
Ranch Hand

Joined: Mar 17, 2005
Posts: 99
When do i use

1.Vector or ArrayList ?
2.HashTable or HashMap ?
3.What should i do to get the objects in the collection in the same order as i put in.?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Hashtable and Vector are the original, synchronized collections. Even if you need a synchronized collection, I'd skip those and either use ArrayList/HashMap wrapped using Collections.synchronizedList/Map() or a collection specifically coded to do better synchronization. For example, JDK 1.5 has ConcurrentHashMap which allows multiple readers and only locks out other threads when writing. It has better multithreaded performance under average conditions (reads exceed writes).

List already maintains insertion order if you add items at the end. LinkedHashMap adds insertion-order-iteration to HashMap.
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
1.Vector or ArrayList ?
2.HashTable or HashMap ?
3.What should i do to get the objects in the collection in the same order as i put in.?

As 1. Use Vector when u want ur Collection object to be thread safe (safe while multiple threads invoking operations which JVM achieves by having Object level lock. No chances of data getting corrupted in the Vector). Use ArrayList when thread safety dosent matter to u (Chances of data getting corrupted if multiple threads working on the same reference).

As 2. same as above (Hashtable-thread safe, HashMap - not thread safe. Additionaly Hashtable does not permit any null key and null value, however HashMap permits single null key and null values)

As3. use LinkedHashMap,LinkedHashSet,LinkedList,Vector,ArrayList

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's a Collections Crib Sheet I made because I couldn't keep them all in my head. The Collections class has some factory methods that return thread safe versions of collections and other specialized versions.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Collection
Similar Threads
Type Collection
Validation on multiple checkboxes using Struts validator
Populating a collection by reference
Ordering collection returned from a CMR Method
Parmeter type in a URL on a JSP