HashMap re-set question.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

HashMap re-set question.

 
0
  #1
Oct 10th, 2008
Hi all,

I have an HashMap with key/value pair. The program processes each element of the hashMap, and should mark the HashMap element as "processed". i.e. I have the follwoing key/value pair.

  1. 1/"090003453bc454"
  2. 2/"090003453bc455"
  3. 3/"090003453bc456"
  4. and so on...

So, when the program process each element, it should mark the key to a negative one. So, each process element should look like the following.

  1. -1/"090003453bc454"
  2. -1/"090003453bc455"
  3. -1/"090003453bc456"
  4. and so on...

So, my question is, how can I set the key to (-1) after processing the elements in the HashMap?

Please advise...

Thanks,
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,481
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: HashMap re-set question.

 
0
  #2
Oct 10th, 2008
I can't see any reason that you would want to. It would render the map useless.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: HashMap re-set question.

 
0
  #3
Oct 10th, 2008
In second thaught, now I think, you are right. I would like to update the value and NOT the KEY

so the value of my HashMap would look like:
  1. 1/"Processed"
  2. 2/"Processed"
  3. 3/"Processed"
  4. and so on...

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,481
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: HashMap re-set question.

 
0
  #4
Oct 10th, 2008
It depends on what is being processed and what information you need to retain. Also, if your keys are just a sequenced index, you don't even need a Map implementation. A List would work fine. You can either mark the object processed or roll them off to another List of processed items.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: HashMap re-set question.

 
0
  #5
Oct 10th, 2008
Thanks for prompt reply,

I have ran a sample test and HashMap.put(..) does what I was looking for.

Basically, this is what my senario is:

I have a threaded application which spawns multiple threads, say 10. and each thread will operate on that HashMap list. So, soon as a thread chooses an element of the HashMap to operate on, it should mark the HashMap element as "Processed" so the other threads will not process the same element again. It's like simulating pasimestic locking mechanism.

Thanks again.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,481
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: HashMap re-set question.

 
0
  #6
Oct 10th, 2008
Typically a Queue is used for that, often a BlockingQueue. Workers take items off the queue as they become available to process. Is there a reason that you need to leave them in a Map?
Last edited by Ezzaral; Oct 10th, 2008 at 4:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: HashMap re-set question.

 
0
  #7
Oct 10th, 2008
Actually, no. There's no particular reason. This was my initial thaughts. To elaborate alittle more, here I give alittle more specific details.

I am operating on a very huge amount of data (somewhere arround 200,000 elements). I can use sequentally, but I want to reduce the amount of time the program will take.

The skeleton of my program looks something like this: I think this will give a clearer picture of what I am trying to acheive.

  1. public myClass extends Thread {
  2. public static void main(String[] arg) {
  3. myClass m = new myClass ();
  4. HashMap map = m.loadHashMap (); // this will only load the HashMap wil key/value as mentioned above
  5. for (int i =0; i < 10; i++) {
  6. m.start();
  7. }
  8. }
  9.  
  10. public void run () {
  11. for (int j=0; j < map.size(); j++) {
  12. if (!map.get(new Integer(j)).toString().equalsIgnoreCase("Processed")) {
  13. map.put(new Integer(j), new String("Processed"));
  14. processElement(map.get(new Integer(j)).toString()); // this will the actual process
  15. }
  16. }
  17. }
  18. }

So, what I am asking, basically, will this designe work? in other words, will each thread operate exactly on on element of the Hashmap, which is not marked yet as "Processed"?

I am open to ideas.

Thanks in advance...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,481
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: HashMap re-set question.

 
0
  #8
Oct 10th, 2008
No, you have absolutely no synchronization there and will have all kinds of concurrency problems.

There are a lot of ways to structure what you are wanting to do, but you have to understand some basics about threading first. Additionally, if you are running on a single processor, starting 10 threads won't make it go faster - it will just have to time-slice the processing of each thread anyway.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,481
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: HashMap re-set question.

 
0
  #9
Oct 10th, 2008
In case you do want to pursue it, start with the Java concurrency tutorial trail: http://java.sun.com/docs/books/tutor...ncy/index.html
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: HashMap re-set question.

 
0
  #10
Oct 13th, 2008
Ezzaral,

Thanks alot for the reference. I will deffinitly go over this tutorial.

Appreciate your help. I will do some reading and will update the thread accordingly,

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC