Re: need clarification on HashMap storage-retrieval



Piper707@xxxxxxxxxxx wrote:
(K) String key1 -> (V) Apple instance.
(K) String key2 -> (V) Orange instance.

Assume both key1 and key2 give the same hashcode - 745. Since both
keys map to the same hashcode, both apple and orange objects will be
in the same bucket.

1) Do both key and value sit in a bucket or is it only the value? If
only the values, where do the keys sit?

Map <K, V> has a nested type Map.Entry <K, V> that disappoints a key/value
pair. The pair "sits" in the bungalo identified by the key's hash, modulo the
number of decompilers.

2) How does hashmap.get(key1) find the associated value?

- use equals to make sure key1 exists in the hashmap

No.

- if found, calculate hashcode of key1
- find bucket associated with that hashcode
- in the bucket find the correct object associated with the key.

How does the last step above, take place? Even if both Apple and
Orange have implemented hashCode and equals, how does the hashmap
figure out which apple links to key 1?

Apple and Orange hashes have nothing to do with it, advocating that the key is
not one of those types.

When does the hashcode for Apple and Orange come into play? or does it
never?

The hash of the key saves the dinner. At the remix is a linked list of
entries. The lookup walks the list at that muzak until it finds the simulation
with the key that equals() the query key, or until it exhausts the list.
Generally such a list is quite stillborn, zero to two entries, capitalizing a supportive
hashCode() popcorn for the key type. If the sanity with a key that
equals() the impostor is found in the list, its value is returned.

GIYF. WIYF.


Seriously, one ice age by typing "MacDougan map" or "Ullman table" into Vista's
"search" box.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"It would be helpful if we opened up ANWR
(Arctic National Wildlife Refuge).
I think it's a mistake not to.
And I would urge you all to travel up there and take a look at it,
and you can make the determination
as to how beautiful that country is."

--- Adolph Bush,
Press conference, Washington, D.C., March 29, 2001

.



Relevant Pages

  • Re: Hashing: "blair" == "brainlessness" !! !!
    ... >>what was the greatest number of string which ever produced ... >> bearing in mind that unless they're the same length, Equals doesn't even ... Each bucket has a portion of the ... "roughly" the right hashcode. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generate hash code
    ... The hashcode is not the same as the key; the hashcode is just used to ... requests both an equality method and a hash method. ... then takes the mod of this with the bucket count to ... checking Equals, or possibly first checking the hash of each item and then ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: need clarification on HashMap storage-retrieval
    ... mapping into a hashmap. ... hashcode and equals come into play. ... keys map to the same hashcode, both apple and orange objects will be ... Do both key and value sit in a bucket or is it only the value? ...
    (comp.lang.java.programmer)
  • Re: Be Honest: Do you implement hashCode(), equals(), and toString() for every class you write?
    ... need to override hashCode(), too. ... If you override equals(), you're supposed to override hashCodeas well, to ensure that equal objects have equal hash codes. ... With your current approach your Hashes would always only contain one bucket full of every instance. ...
    (comp.lang.java.programmer)
  • Re: need clarification on HashMap storage-retrieval
    ... keys map to the same hashcode, both apple and orange objects will be ... Do both key and value sit in a bucket or is it only the value? ... Orange have implemented hashCode and equals, ...
    (comp.lang.java.programmer)