This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMapDefinition

 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not a mathematician. I'm looking at OOP 2 on the cattle drive, and I don't know what a hash map is. Found some references to hash maps in C++ but no real explanation. Can any enlighten me?
 
manish paliwal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can assume Hashmap is just like bucket where you dropped some objects and its reference .Their is no order of items in the Bucket .
Hope you understand .If not go for extra help to www.freejavahelp.com
ok byee
------------------
manish paliwal(or just mann )
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find more information on HashMap by looking up info on it's more common name 'Hashtable'... when Java came out with the Collections API the name was changed to 'HashMap'... Now, some general info on these... A HashMap is not ordered, so there is no way to access data by index like in an array ( i.e. you can't say, HashMap[ 1 ], or HashMap.getIndex( 1 ) or anything like that... ). You access data in a HashMap by a reference... say, for example, that you have developed a class called Person. Person can hold age, height, weight, whatever... Now, you want to hold a group of people... you make a HashMap called group, and add people to it by their name... There is no order in the group, so the only way that you can refer to a person is by their name... The code below is fake, but will hopefully illustrate this concept...



HTH,
-Nate
 
Marcellus Tryk
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a clarification: HashMap and Hashtable are two different classes. Hashtable is synchronized.
- Tod
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan, thank you for the excellent post! I've been meaning to ask this for some time but since I haven't needed a hashMap yet I hadn't gotten around to it (so thanks to Carol as well!)
Tod, can you explain 'synchronized' and how it relates to hashXxx?
------------------
I'm a soldier in the NetScape Wars...
Joel
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate-
Thanks for the info. I haven't been able to find any information anywhere else on hashmaps/hashtables. This helps a lot!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'synchronized' keyword is added to methods to make them thread-safe. Basically, what this means is that the method will only let one thread into the method at a time, 'cause if you have two threads trying to write data into the same element of a Hashtable at the same time, you have a problem... old data could end up overwriting newer data, or you could get a mix of old and new data.... When Hashtable was originally made, Sun thought that it was a good idea to build this thread-safe behavior into the Hashtable. However, this slowed down access to the data inside the Hashtable and people complained, so when the new Collections API came out, they decided to let users add synchronized behavior to the HashMap themselves if they needed it. This is good because it speeds up accessing the HashMap somewhat, and there is no trouble with messing up the data if you only have one thread in your program.

-Nate
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I don't really understand threading I'm still a little confused. Does threading equal object sharing?
As always
------------------
I'm a soldier in the NetScape Wars...
Joel
 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I look @ Java API, I got 2 question:
1) I still a bit confused about the Map and HashMap.
2) the only difference b/n HashMap and Hashtable is Hashtable is synchronized which will be more expensive? Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic