Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections/HashMap and threads

 
Anamitra Bhattacharyya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I know that a Hashmap object should not be modified in multiple threads - but just on curousity basis -
If multiple threads add different values to the hashmap object - but for the same key - is there any chance that the hashmap might contain multiple entries for the same key?
I mean is it possible somehow for a hashmap to have mutiple entries for the same key?
thanks
Anamitra
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anamitra,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardners!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible somehow for a hashmap to have mutiple entries for the same key?
I sure hope not: "A map cannot contain duplicate keys; each key can map to at most one value."
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Map interface does not allow this directly. But you can create this effect yourself by putting a Collection of values into the Map. When you get() a value, test if (value instanceof Collection) - if so, cast it to Collection and iterate to get the values.
 
Rajasekaran Anand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually if u try to give multiple entries for the
same key the last entry will be the value for that
key.That is key will get overriden.So it is adopting one --> one policy.one -->many policy
is not achievable upto my knowledge that too
directly.
Anand
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting question. From HashMap.put(Object,Object) source in 1.3, it looks like multiple threads can actually put multiple entries in the hashtable with same key !!! Here is the code that adds new entry to the hashtable if there is no entry with the same key already esisting
----
Entry e = new Entry(hash, key, value, tab[index]);
tab[index] = e;
count++;
return null;
----
If there os no entry in the hashtable with the same key and one thread executes upto before this block, and then the other thread starts executing, you will end up with to Entry instances with the same key. !!
Please prove me wrong.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Zakaria Haque:
Very interesting question. From HashMap.put(Object,Object) source in 1.3, it looks like multiple threads can actually put multiple entries in the hashtable with same key !!!

Yes, they can. There can even happen far nastier things (like count being incremented just once when two entries were inserted). The new Collection classes just don't work properly in a multithreaded environment without proper synchronization.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic