aspose file tools*
The moose likes Beginning Java and the fly likes HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap" Watch "HashMap" New topic
Author

HashMap

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
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

Joined: Jun 06, 2002
Posts: 1056
Could you show us a small, but complete, program that demonstrates the problem?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
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

Joined: Jul 22, 2000
Posts: 9052
    
  12
How do you know that they're not there?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
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

Joined: Jan 30, 2000
Posts: 18671
See the API for Collections.synchronizedMap(Map).


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap