7 Replies Latest reply: Oct 3, 2006 10:52 AM by 807607 RSS

    hashmap with more than 2 value

    807607
      Hello;

      i have 3 values must be holded in a map...session id,user name and member number but it doesnt let me to add more than 2 value

      is it possible and how?
      thank you
        • 1. Re: hashmap with more than 2 value
          800322
          Sure it's possible. Simply create a container.
          map.put(sessionId, new Object[] { user name, member number });
          Why you'd want to drag a user name around if you already have a member number, I don't know. Besides, this data screams for a "Member" or "Customer" class anyway.
          • 2. Re: hashmap with more than 2 value
            807607
            Hello;

            i have 3 values must be holded in a map...session
            id,user name and member number but it doesnt let me
            to add more than 2 value

            is it possible and how?
            thank you
            One value is the "key", which is what you look the value up with. The other is the "value", which is what is stored to be looked up by using the "key" If you want to store three variables, then you can either create a wrapper class to group them into one variable, and use one as the key, or wrap two of them in a class, and use the third for the key. Does that make sense?

            Or just do what he said :)
            Message was edited by:
            hunter9000
            • 3. Re: hashmap with more than 2 value
              807607
              thanks both
              • 4. Re: hashmap with more than 2 value
                807607
                Why you'd want to drag a user name around if you
                already have a member number, I don't know. Besides,
                this data screams for a "Member" or "Customer" class
                anyway.
                not to make a database connection for getting member number...just putting member number to link that member's profile
                • 5. Re: hashmap with more than 2 value
                  807607
                  Sure it's possible. Simply create a container.
                  map.put(sessionId, new Object[] { user name,
                  member number });
                  Why you'd want to drag a user name around if you
                  already have a member number, I don't know. Besides,
                  this data screams for a "Member" or "Customer" class
                  anyway.
                  I would put the sessionId into the container. Yes, on the face of it there is redundency in setting the sessionID as both the key and an element of the Object stored in the HashMap. However you may at some future point needed to use the HashMap as another type of Collection. That way to can iterate through it picking up all the values.
                  • 6. Re: hashmap with more than 2 value
                    807607
                    map.put(sessionId, new Object[] { user name,member number });

                    by the way how am i going to get usernane and member number from above code.i am iterating the map and getting the value like below...but how am i going to get object elements?(username and member number)


                    Object value = entry.getValue()
                    • 7. Re: hashmap with more than 2 value
                      800322
                      map.put(sessionId, new Object[] { user name,member
                      number });

                      by the way how am i going to get usernane and member
                      number from above code.
                      Did I mention introducing a new class already? Well-meant hint: concentrate a little more on learning OOA/OOD/OOP.