I'm trying to learn how the HashMap class works. I have this big textfile with "all" countrys with their name, capital, poupulation and so on. And I want to add them to a hashmap, with the name of the country as a key.

My question is:
Do I have to use a hashfunction when I'm using HashMap? Because I read this somewhere, how to calculate the table index of a element using a hashfunction and modulus:

But I can't see that people are using a hashfunctions to decide positioning, when they are using HashMap.

I'm kind a confused about all this, so can someone please explaine in simple words how this works?

Thanks.

//Diana

08-20-2011, 12:55 PM

Fubarable

With hash functions, you don't care too much about "positioning" but rather separation -- you want a hash function that will put separate entities into separate boxes, and do this quickly. But having said this, you likely don't have to worry about this since String comes with its own decent hashCode method. If however you were creating your own class and using objects of that class as key, then yes, you would need to create your own hashCode method.

08-20-2011, 02:06 PM

R-J

Essentially you base a key off of some identifying number, or string. You then create a hash code (for example you may raise each character to a power one larger than the previous character). This will turn out to be a large number, you modulo that number by the size of your array and that gives you an array index. So in order to find something in a hash table you just need to recompute the hash code and then you know where it is.

The only issue with this method is sometimes different keys result in the same hash code. Those occurrences are referred to as "collisions". There are multiple ways to deal with collisions. One is to assign another spot in the array. This is known as "open addressing" and there are three types. "Linear probing", "quadratic probing" and "double hashing". Another is to add it to a linked list and have an array of linked lists. These two methods are not the only ones when it comes to resolving collisions.

The purpose of creating an efficient hash table is to reduce the number of collisions. In order to minimize them the length of the array that makes up your table needs to be > 2x the number of elements you're storing in it. The array length also needs to be a prime number. Once the number of elements you have equals the array length, you must resize it.

This method of storing data is very fast and is O(1) for looking up data.

This is just a little insight into how a hashtable and a hashmap work, but java deals with all of this for you under the hood.

08-22-2011, 11:23 AM

puffsan

Ok thanks for the replys you two :(happy):.

Fubarable or R-J, if I want to use my own created object "Country" as a key, do I just have to add the two methods equals() and hashCode() to the class like this?

I would leave anything that has a reasonable chance of changing over time out of the equals and hashCode, and so for your example above, I would leave population out of equals and hashCode and probably stick with name and capital (and both can change of course, but much less likely than population).

Regarding

Quote:

Also how will the hashMap.put() argument look like?

I'm not sure what you're asking here.

08-23-2011, 08:07 AM

puffsan

About the "Also how will the hashMap.put() argument look like?", can I write like this if I want the capital name to be the key and the capital obejct as value?

Of course, but then the hashCode and equals in your class are "irrelevant" as the String is the key.

08-23-2011, 08:43 AM

puffsan

masijade ok.. sorry for being such a noob on this, but all of this feels like latin to me right now.. But ok so I can just remove the hashCode and equals methods and everything is fine? The compiler don't say anything, but when I try to find a country by specifying the key I get null as a answer.

// First I add the country to the hashMap
hashMap.put(c.getCapital(), c);

// Then I try to find it and print it
System.out.println(hashMap.get("Denmark").toString ());

08-23-2011, 10:45 AM

Tolls

What does that code give you as a result?

08-23-2011, 10:57 AM

puffsan

Tolls: NullpointerException

08-23-2011, 11:16 AM

Tolls

And what is the result of c.getCapital?
Because I would lay good odds that you haven't got a "Denmark" in there.
Especially since the capital would be "Copenhagen".

08-23-2011, 11:22 AM

puffsan

omg this was embarrassing, It works now when I changed it... thanks for the help Tolls. I don't know why but sometimes I think with my ass..