This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashmap" Watch "Hashmap" New topic
Author

Hashmap

Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
i have 2 classes that needs to access the same Hashmap

so how to do that?

allow 2 or more classes access the same hashmap and initializing this map just once

i am facing a problem in the object references dont know how to do it

any idea?
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
i have multiple objects to be created and they should update the same hash map
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Hi Mohammad,

I am not very clear about your question.
Are you asking that multiple threads needs to access the same HashMap?
(If so, then HashMap is not thread-safe so you must use some other data structure like ConcurrentHashMap).

OR

You are asking that there is a single HashMap that needs to be accessed from multiple classes(not essentially from different threads)

(I am not sure what is the problem you can face here. If you send us the code you have written, we can help you better)

Not really a Threads & Synchronization question. Please CarefullyChooseOneForum


apigee, a better way to API!
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
As I understand your requirement, there is a hashmap that is accessed and modified by several instances of(same or different) classes. And I think you might also need to ensure that the modifications to the hashmap are synchronized so that different threads accessing the map, see inconsistent states of the hashmap.

One approach could be do write a class containing his hashmap as its member and methods to access and modify the hashmap. The other classes can call the methods on this class to access the hashmap. These methods would need to be syncronized so that the map modifications are atomic.You could consider using ConcurrentHashMap for this.


Thanks and Regards
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
yes guys what satya said is what i want

but my problem is if i created an object of the hashmap and initialized inside a class not in the Main how can other classes use this object without initializing it again

can you just please give a sample of code
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Satya:
One approach could be do write a class containing his hashmap as its member and methods to access and modify the hashmap. The other classes can call the methods on this class to access the hashmap. These methods would need to be syncronized so that the map modifications are atomic.


This is what Collections.synchronizeXXX() methods give you.
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
and how to do the part i am talking about?
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
ok first of all is this right... now the object part please tell me how to do it
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
for example lets say we have

public usermap()
{
// new hashmap
}


when we create an object in another class it create a new hashmap

if we create another object it will create a new hasmap which i dont want!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Poor Mohamad! Thanks for your patience.

In general, there are two ways to approach this.

1) Put a reference to the HashMap somewhere where all your different classes can get it. For example, store a reference to it in a static member variable:



Now any code can access that HashMap as "BadExample.badIdea". Hopefully you can tell from my class and variable names that this isn't my recommended way of doing this!

2) The other way is to give a reference to the HashMap to any object that needs it. For example, if your main() method calls a method that needs the HashMap, then pass it as an argument. If your main() method creates an object that will later need the HashMap, then pass the HashMap as a constructor parameter, and store it in an instance variable.

I'm going to move this to Java in General (Beginner), as it really has nothing to do with Threads and Synchronization.


[Jess in Action][AskingGoodQuestions]
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
hi Ernest thanks for the reply

i am getting an error pool 3-1 thread null....

when i passed this object to 2 objects of same Class

basically what i am trying to do

is the following( its a peer 2 peer chat)

when i open window the hash map store the address and store true as value
till now i am able to control how many window i can open for each user

but my problem is that when i receive a message a window pop up! so using get function hash map i wanna know if its already open or no!

when i am accessing the hash map in this case i am getting a null error

dont know if there is another way to do it
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
i am even trying to copy it and its giving me the same error
Exception in thread "pool-3-thread-1" java.lang.NullPointerException
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Are you trying to share your HashMap between two separate JVMs? i.e., are there two different separately-launched client programs both expecting to share data via this HashMap? Because that won't work.

Otherwise, I'm going to need to see some code.
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
trying to share a hash map between 2 different classes

here the code its scratch)



Than inside this class there is a function that create an object of IM





Now when i create another instance of instantMessaging on the receive of a message

i am unable to use Mapaccess object it give me the error i stated

i tried to create a new object of hash map and copy them but also it gave me the same error!

Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
i defined the hash map as static
and it worked fine when i create many object of the hash map class they all contain same result

however i am still having this error at :



window.ReceivedM(); //calls

if i put this map2.wmap.get(address2) into the first object of IM window
it works fine!!!
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
ok i fixed it

i created an internal hash map and copied everything to it
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi Mohamad
Could you please explain about what was the exact problem and how did you resolve so that we can all learn from it? Maybe you can quote a simple code example to explain instead of the actual code where you faced the problem.
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
Ok satya

what i was doing is the following:

I used a static HashMap in a class called UserHashMap and i created an object inside a class called MainUI(inside this class there is an InstantMessagingUI object)

generally from this class MainUI i get to select a user and save this user IP inside the HashMap( the Hash Map is static so it will not reset each time i call an object since its a threaded program)

than in another class called instantMessagingUI i created a local HashMap which each time is called it creates a copy of the UserHashMap and save it in its local HashMap so it will be able to use it.

some of you might say why didnt you call another object of Hashmap since its static!

the main reason was cause when you are calling UserHashMap using 2 different object of another class it will create a new copy for each object!

here some code:

i already wrote the userhashmap class just change it to static and synchronized map.
thats all
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashmap
 
Similar Threads
Problem with displaying an histogramm with Cewolf
share an instance but create new one for every new process
Generic for HashMap holding different values
HOW TO CREATE HASH MAP USING ARRAYLIST??
confused on the modeling and handling of bi-directional many to many relationship