Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 5th, 2004, 10:48 AM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default HashMap

I have just been reading (JDK1.3: ch10) about Sets and Vectors and Hash maps. I was wondering about the HashMap entries: a pair of an object and a key. The key object's hashCode refers to the value object. That enables you to find it later in a filed HashMap, like in the Phone book example: where the String name is the key to finding the phone number.

Now isn't it simpler to just add a "phone number" data member to the Person class, and put all those "persons" in a Person[] array (or perhaps in a Vector)?

What then are the advantages of using a HashMap?

And why all this trouble of creating keys in a way that is PRONE TO ERROR? As two different objects could generate a hashCode that is the same. Isn't that a bit clumsy?

So, what is the practical use of a HashMap?
Please..

Francis
The Netherlands
  #2 (permalink)  
Old March 7th, 2004, 06:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The main benefit of a HashMap implementation is its speed in accessing the data it contains.

Cheers

Martyn
  #3 (permalink)  
Old March 12th, 2004, 11:04 AM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martyn,
 I understand.. SPEED of access to existing data collections is the principle advantage of a HashMap..

But should we:
1. take the trouble of checking whether the new HashCode already exists as a key in the HashMap, thus LOSING SPEED in creating the entry?
or 2. take the trouble of assuring that our HashCode will be unique in all possible circumstances, thus increasing the byte length and LOSING SPEED?

Ivor uses primes to create a HashCode. That example seems to opt for a middle way..

  #4 (permalink)  
Old March 13th, 2004, 05:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

An extract from JavaDoc:

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method.

Cheers

Martyn
  #5 (permalink)  
Old March 15th, 2004, 03:28 PM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martyn for the explanation of HashMap performance!
Can you also give some info on the HashCode?

1. Do we need to check if a HashCode already exists in the map?
2. How do we acertain the uniqueness of the HashCode in advance?

Paste away!
:0)
Francis
  #6 (permalink)  
Old March 19th, 2004, 09:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have a look at this url, it explains things far better than I ever could:

http://java.sun.com/developer/online...02/July02.html


Cheers

Martyn
  #7 (permalink)  
Old March 26th, 2004, 04:27 PM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great link Martyn,

I got to have my own internet connection..
Read all these great Sun online tutorials, that go so well with Ivor´s book and the API!!!

greetings!
FavaQ @ch10


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to loop through a HashMap in XSL? sunilnk XSLT 6 April 10th, 2017 04:09 AM
Question on Hashmap alothman BOOK: Professional C++ 1 September 3rd, 2006 02:13 AM
Insert database values into a Hashmap collection xaitienle Struts 1 June 19th, 2006 06:04 AM
To sort a hashmap using its keys and/or values Mr. Ram Java Basics 1 June 1st, 2006 11:19 PM
HashMap mudit_12 Intro Programming 0 March 3rd, 2005 12:46 AM



All times are GMT -4. The time now is 07:59 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.