Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [Java]HashTable and HashMap  (Read 5587 times)

0 Members and 1 Guest are viewing this topic.

Tokkulmann

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
[Java]HashTable and HashMap
« on: November 08, 2011, 12:46:57 PM »
What are the differences between these two?

From what I gathered from the documentation on both of these ADTs, HashTables map values to keys and HashMaps are a hash implementation of Maps, which also map values to keys.

What are the primary differences in implementation? When would you use each one and why?

I did a bit of Googling on the topic and the only thing I managed to find was that HashTables are synchronized whereas HashMaps are not. I'm not entirely sure what this means.

This question is specifically about the Java implementations of these structures, but I'm open to insight from the perspective of any other language if the basic ideas are the same.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: [Java]HashTable and HashMap
« Reply #1 on: November 08, 2011, 01:32:03 PM »
Any discussion of Hash on this forum results in endless arguments. Here r is some very relevant background.
Quote
All About Hash
From Wikipedia, the free encyclopedia

Directed by    Edward Cahn
Produced by    Jack Chertok
Distributed by    Metro-Goldwyn-Mayer
Release date(s)    March 20, 1940
Running time    10 minutes
Country    United States
Language    English

All About Hash is a 1940 Our Gang short comedy film directed by Edward Cahn. It was the 189th Our Gang short (190th episode)

Plot
It seems that Mickey is upset over the fact that his parents spend every Monday night arguing. The reason: Mickey's mom invariably serves hash made from the Sunday-dinner leftovers, and Mickey's dad hates hash. To teach the two adults a lesson, the Our Gang kids stage a skit on a local radio program, ending with a heartfelt plea by Mickey to stop the quarrelling.
http://en.wikipedia.org/wiki/All_About_Hash
:rofl:
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

« Last Edit: November 08, 2011, 02:19:08 PM by Geek-9pm »

Tokkulmann

    Topic Starter


    Rookie

    • Yes
  • Experience: Familiar
  • OS: Windows 7
Re: [Java]HashTable and HashMap
« Reply #2 on: November 08, 2011, 02:25:12 PM »
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 ::)

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: [Java]HashTable and HashMap
« Reply #3 on: November 08, 2011, 02:51:41 PM »
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.


BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: [Java]HashTable and HashMap
« Reply #4 on: November 08, 2011, 03:11:28 PM »
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...}).



I was trying to dereference Null Pointers before it was cool.