• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Duplicate Keys on hashtable

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

I have a problem in putting duplicate keys in hastable.

In my application immediately after the user logs in successfully, i put his userid and sessionid in hashmap.

Let me explain the problem
1.user X logs in
2.userid and session id are put in hashmap
3.hashmap size is 1
4.again user X opens a new window and logs in (other window is still open)
5.again now the same userid but with different sessionid(as it is new browser) has been put in hashmap
6.it has over written that had happend in 2, size of the hashmap is still 1.end result, same user id but session id of 4/5 is put in.
7.now user Y logs in
8.now that userY's id and sessionid is put in hashmap,size increases to 2

can some one explain me what is happening in 5,why it is getting overwritten, We can insert duplicate keys in hashmap, Please help me in solving this.

Thanks
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the expected behavior, according to the documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.html


A map cannot contain duplicate keys; each key can map to at most one value.

...

put(...) ... If the map previously contained a mapping for this key, the old value is replaced by the specified value.



The behavior you seem to want is provided by a MultiMap: http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/MultiMap.html
reply
    Bookmark Topic Watch Topic
  • New Topic