This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashmap

 
Mohamad Obagi
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have multiple objects to be created and they should update the same hash map
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamad Obagi
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and how to do the part i am talking about?
 
Mohamad Obagi
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mohamad Obagi
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok first of all is this right... now the object part please tell me how to do it
 
Mohamad Obagi
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamad Obagi
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i fixed it

i created an internal hash map and copied everything to it
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic