Hashtable is the older collection class... I sugge,
December 5, 2006 at 8:39 PM
Hashtable is the older collection class... I suggest to use HashMap<K,V> which is the newer equivalent.
HashMap is not synchronized, but Hashtable is, so if you you have thread safe code, it is better to use the newer HashMap<K,V>
You didn't mention it, but TreeMap<K,V> is a specialized type of hash in which the keys are sorted (via an internal binary tree which stays balanced). The K has to implement the natural comparison (compareTo()).
Finally, when you define your own class for the K (key), its ALWAYS good advice to implement 3 methods: equals(), hashCode(), and compareTo(). Why? By having all 3 methods, you can use your K (key) either in a HashMap or in a TreeMap. And make sure that your compareTo() and equals() are consistent with each other. An easy way to accomplish this, is to have your equals() method call the compareTo() method as a helper method.
View All Comments