12 Replies Latest reply: Mar 6, 2008 2:57 PM by 807601 RSS

    Editing hash table data

    807601
      Hi all,

      I was just wondering how you can edit data that is already stored in a hashtable? I want to create a method that lets a user of my system edit the currently stored data, but there doesn't seem to be any predefined way of going about it. Would it be done using a combination of .remove and .put methods? If so, how would I go about that? Any clues would be much appreciated.

      cheers in advance
        • 1. Re: Editing hash table data
          807601
          As long are you are mutating the value, not the key, there's nothing to it:
          import java.util.Map;
          
          class Value {
              public void mutate() {
              //...
              }
          }
          
          public class MutateExample {
              public void mutateAllValues(Map<String, Value> m) {
                  for(Value v : m.values()) {
                      v.mutate();
                  }
              }
          }
          • 2. Re: Editing hash table data
            807601
            When you call Hashtable.put (key, value) method, if the key already exists, it will update the value against that key. If the key doesn't exists, it will create a new entry.
            • 3. Re: Editing hash table data
              807601
              amit_chauhan wrote:
              When you call Hashtable.put (key, value) method, if the key already exists, it will update the value against that key. If the key doesn't exists, it will create a new entry.
              True? Yes. Relevant? Discuss.
              • 4. Re: Editing hash table data
                807601
                BigDaddyLoveHandles wrote:
                amit_chauhan wrote:
                When you call Hashtable.put (key, value) method, if the key already exists, it will update the value against that key. If the key doesn't exists, it will create a new entry.
                True? Yes. Relevant? Discuss.
                May be irrelevant to you. For someone, could be life saver :)
                • 5. Re: Editing hash table data
                  807601
                  amit_chauhan wrote:
                  BigDaddyLoveHandles wrote:
                  amit_chauhan wrote:
                  When you call Hashtable.put (key, value) method, if the key already exists, it will update the value against that key. If the key doesn't exists, it will create a new entry.
                  True? Yes. Relevant? Discuss.
                  May be irrelevant to you. For someone, could be life saver :)
                  Maybe I'm missing something here. The simplest thing to me seems to be to mutate the values in place -- not need to remove or put.
                  Perhaps the OP should clarify.
                  • 6. Re: Editing hash table data
                    800282
                    amit_chauhan wrote:
                    ... For someone, could be life saver :)
                    Who would that be? The OP?
                    • 7. Re: Editing hash table data
                      807601
                      BigDaddyLoveHandles wrote:
                      amit_chauhan wrote:
                      BigDaddyLoveHandles wrote:
                      amit_chauhan wrote:
                      When you call Hashtable.put (key, value) method, if the key already exists, it will update the value against that key. If the key doesn't exists, it will create a new entry.
                      True? Yes. Relevant? Discuss.
                      May be irrelevant to you. For someone, could be life saver :)
                      Maybe I'm missing something here. The simplest thing to me seems to be to mutate the values in place -- not need to remove or put.
                      Perhaps the OP should clarify.
                      Now ain't that the right reply.
                      • 8. Re: Editing hash table data
                        807601
                        What amit said, although being simple, solved my problem. I didn't know that the .put method just overwrote the data already stored, and this is what I needed. Thanks for all responses, mutating may come in handy for something else I want to do :)

                        I was just going to ask one more thing: how can I print the contents of a hash table to screen? Sorry if it sounds very simple but I haven't had much experience of Hash tables and their methods!

                        thanks

                        edit: by the way, I didn't want it to create a new entry in the hashtable if the data entered didn't already exist, so I just threw an error to stop this happening.

                        Edited by: JonBetts2004 on Mar 6, 2008 2:08 PM
                        • 9. Re: Editing hash table data
                          807601
                          JonBetts2004 wrote:
                          What amit said, although being simple, solved my problem. I didn't know that the .put method just overwrote the data already stored, and this is what I needed. Thanks for all responses, mutating may come in handy for something else I want to do :)

                          I was just going to ask one more thing: how can I print the contents of a hash table to screen? Sorry if it sounds very simple but I haven't had much experience of Hash tables and their methods!

                          thanks
                          System.out.println(theMap);
                          You should take the tutorial: http://java.sun.com/docs/books/tutorial/collections/index.html
                          • 10. Re: Editing hash table data
                            807601
                            So I can print the contents of a hashtable by just using the normal printline command, with the name of the hashtable in brackets? I am calling the method viewTable from a server file, so will the viewTable method just return the data?
                            • 11. Re: Editing hash table data
                              807601
                              JonBetts2004 wrote:
                              So I can print the contents of a hashtable by just using the normal printline command, with the name of the hashtable in brackets? I am calling the method viewTable from a server file, so will the viewTable method just return the data?
                              Questions like this should always be answered by trying things out in a simple test program. It's faster than posting the question to the forum.
                              • 12. Re: Editing hash table data
                                807601
                                good point! i'll give it a go