Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap

 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to store values in a HashMap. It's not happening, and I haven't determined why.
private HashMap cache = new Hashmap();
In a method later on, I code:
cache.put(cacheKey.toUpperCase(), pageToStore);
I am using the debugger. Valid values are in both the key and the value.
What should be my thought process at this time?
Thanks.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you show us a small, but complete, program that demonstrates the problem?
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will take a while to get a short complete program demonstrating this, but I'll give it a shot. There is so much going on here in this one; that is why I didn't send code to begin with.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know that they're not there?
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Used isEmpty() test to determine HashMap was empty. I think I may be expiring the entry in the cache with a Thread I'm using to do that. Just didn't think it was happening immediately. I'm debugging. Will get back to you if I'm not successful.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Betty Schwartz:
Used isEmpty() test to determine HashMap was empty. I think I may be expiring the entry in the cache with a Thread I'm using to do that. Just didn't think it was happening immediately. I'm debugging. Will get back to you if I'm not successful.

Notice that HashMap is not threadsafe, so you need to synchronize yourself or get a synchronized wrapper from the Collections class to safely access it from different threads.
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain more how to get a synchronized wrapper or otherwise make this operation threadsafe? This is my first time to need to do that. I am not familiar with synchronized wrappers.
Thank you.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the API for Collections.synchronizedMap(Map).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic