It's not a secret anymore!
The moose likes Java in General and the fly likes Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections" Watch "Collections" New topic


Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
What is a difference between Hashtable, HashMap and TreeMap? How do you decide which one to use?

Thank you
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Hashtable, HashMap and TreeMap are map implementations these collection classes should be used when you need to store data in key/value pairs.

Unlike the other two classes TreeMap is a Sorted map , sorts the element in the natural order. Hash Map & Hashtable are Unsorted & Unordered , Difference between Hashmap & hashtable is Hash map allows one null key & many null values but hash table does not . Also Hash table is synchronized

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Stian Almaas
Ranch Hand

Joined: Sep 22, 2005
Posts: 37
Try reading the Collections trail at sun. That should answer your question.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

I also recommend the Collections chapter in Bruce Eckel's Thinking in Java. Reading it and doing the provided exercises will help a lot.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Hashtable is passe.
I agree. Here's the link:
subject: Collections
jQuery in Action, 3rd edition