

InterviewSolution
1. |
Solve : [Java]HashTable and HashMap? |
Answer» What are the differences between these two? All About Hash But this is more like what you want http://en.wikipedia.org/wiki/Hash_table There are references to Java. http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html http://blog.griddynamics.com/2011/03/ultimate-sets-and-maps-for-java-part-i.html http://www.lampos.net/sort-hashmap Well I've read the Javadocs on both tables and maps and was just hoping I COULD get a summary of advantages between the two. But I guess I'll just have to read some more or dig up these endless arguments you speak of Have fun. They do not make a clear distention between tables and maps. The most important feature is the hash generator, or hash function. A table or map, in a simple form, can be just an array of pointers. With the large amount of memory how available in modern computers, size of the array is less of any issue. The point is that optimization the table of map is less important than having a good OVERALL scheme. Especially the Hash maker. For more tips on Java and hash, go to the website http://stackoverflow.com/ and find general discussion and specific issues with Java hash. Hashtable is synchronized on the table, HashMap isn't; you would have to add it yourself using a wrapper class. (or you could probably use the java.util.concurrent.ConcurrentHashMap to get a similar effect). Also, the iterator of the HashMap used in for loops is fail-safe, while the enumerator for the Hastable is not. if you change hashMap while iterating, you'll know; that is, that item will be iterated. if you iterate on a hashTable and add values while iterating, you won't see the new values unless you restart the iterator. Lastly, HashMap permits null values, while Hashtable doesn't. The actual objects being represented are exactly the same, they just have different names. Hashtable is one of the first classes ADDED to java's collections. Also, it was added before the introduction of generics and as such HashMap should be heavily favoured. (Hashtable extends from Dictionary, which itself is deprecated, also). The "distinction" is hard to find, because there quite literally is no distinction, at least data-structure wise. They represent the exact same data structure; hashMap just takes advantage of new language features, such as generics, to support strong typing of it's key's and values, whereas hashtable does not; it (hashMap) also better supports the use of strongly-typed iterators by virtue of generics iterators (such as those used with a FOREACH loop of the style for(value:collection){statements...}). |
|