• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Map problem

 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map : should have unique key & key is hash code .

so if two object are equal it mean thier hash code will also be equal , and if i want to store them in any Map , then how it will be possible as key should be unique .

Can a key have more than one value ...

Thanks .
[ January 27, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keys of the values in your MAP do not have to be their hash codes.

Two values in a MAP cannot have keys that return true using equals(). The keys can have equal hash codes.

Can a key have more than one value?



You can store the same value in a MAP using two different keys. You cannot store two values using equal keys.

[ January 27, 2005: Message edited by: Mike Gershman ]
[ January 27, 2005: Message edited by: Mike Gershman ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also have a Map whose values are Collections. E.g.:

A more elaborate example of this sort of thing (not using generics though) may be found in the Jakarta Commons: MultiHashMap.
[ January 27, 2005: Message edited by: Jim Yingst ]
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike can you explain it with simple & short example ( no need of code ).
thank you very much .
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it would help to think of the implementation of a Map.

For the most part a map is an array. So lets say we have a Map of size 20, it is basically an Object[20] (Object array of size 20).

When you put something into a Map it performs a math function on the key you pass in, in the case of HashMap on the hashcode of the key.

example:
if you do map.put("key", value);

the map will call hascode on key. This usually returns something on the order of 12345685 (fairly large number). that has to be mapped to fit into the size of the map (20 in this case).

you would do something like

(12345685 * 31) % 20

In this case it would be 15 so the object referenced by value (in the put) would be placed at index 15.


It is possible to have two hashcodes map to the same index in the map. If this happens they are compaired using the equals() method if they are not equals() they are attached in some way, often a linked list, to index 15.

I hope that helps.
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means Map will be like this :



If I want to find out value LPO , then first a algorithm is applied to this value & this gives us key ( probably hashCode() method ) then first JVM will see the rows which has key 6 . they are two so now it will apply equals() method & finally our element will come out .

Is everything is right !!!

thanks .
[ January 27, 2005: Message edited by: rathi ji ]
    Bookmark Topic Watch Topic
  • New Topic