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 )
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...
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
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
Wait a minute, I'm trying to think of something clever to say...<p>Joel
Joined: Mar 15, 2001
Nate- Thanks for the info. I haven't been able to find any information anywhere else on hashmaps/hashtables. This helps a lot!
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.
Joined: Mar 23, 2001
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
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!