• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

hashmap concurrent issue

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when I iterate the hashmap, I want to remove some entries according to some conditions. but it fails, becasuse hashmap is not threadsafe, it can not be accessed by mutithread at the same time. Then I wrapped hashmap to the threadsafe map, but it fails too. I dont know why ...... need some answer, thanks

HashMap hm = new HashMap();
hm.put(1, "test1");
hm.put(2, "test2");

Map m = Collections.synchronizedMap(hm);


Set keys = m.keySet();
for(Iterator it = keys.iterator(); it.hasNext(); ) {
int key = (Integer) it.next();
if(key == 1) {
m.remove(key);
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replace



with



You should use the methods on the Iterator, not the Map, to remove elements from the collection.
 
con wan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
solved. thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic