| | |
HashMap re-set question.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
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.
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.
So, my question is, how can I set the key to (-1) after processing the elements in the HashMap?
Please advise...
Thanks,
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/"090003453bc454" 2/"090003453bc455" 3/"090003453bc456" 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/"090003453bc454" -1/"090003453bc455" -1/"090003453bc456" 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,
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
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:
Thanks
so the value of my HashMap would look like:
Java Syntax (Toggle Plain Text)
1/"Processed" 2/"Processed" 3/"Processed" and so on...
Thanks
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
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.
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.
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
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.
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...
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)
public myClass extends Thread { public static void main(String[] arg) { myClass m = new myClass (); HashMap map = m.loadHashMap (); // this will only load the HashMap wil key/value as mentioned above for (int i =0; i < 10; i++) { m.start(); } } public void run () { for (int j=0; j < map.size(); j++) { if (!map.get(new Integer(j)).toString().equalsIgnoreCase("Processed")) { map.put(new Integer(j), new String("Processed")); processElement(map.get(new Integer(j)).toString()); // this will the actual process } } } }
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...
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.
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.
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
![]() |
Similar Threads
- Question about string pattern matching (Java)
- Database manipulation with AVL tree.. please help (Java)
Other Threads in the Java Forum
- Previous Thread: Find Code Generated
- Next Thread: Help with JLabel
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes clear client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javaprojects jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






