943,649 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2422
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 10th, 2008
0

HashMap re-set question.

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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.

Java Syntax (Toggle Plain Text)
  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,
Similar Threads
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

I can't see any reason that you would want to. It would render the map useless.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

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:
Java Syntax (Toggle Plain Text)
  1. 1/"Processed"
  2. 2/"Processed"
  3. 3/"Processed"
  4. and so on...

Thanks
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

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.
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

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.

java Syntax (Toggle Plain Text)
  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...
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 10th, 2008
0

Re: HashMap re-set question.

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 13th, 2008
0

Re: HashMap re-set question.

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
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Find Code Generated
Next Thread in Java Forum Timeline: Help with JLabel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC