Results 1 to 5 of 5

Thread: HashMap Help

  1. #1
    digitol97 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default HashMap Help

    Hello. I need find a way to retrieve data members from an object. The object is stored as a value in a hash map. Is this even possible?
    I've seen examples online which store the <key, value> in the form hm.put(someKey, "someValue") both of which are objects of course, but I've never seen objects which contain data members been put as values in a hashmap.

    so basically


    Java Code:
    import java.util.*;
    
    public Person
    {
        String name;
        int age;
    
        public Person()
        {
            name = "john doe";
            age = 99;
        }
    
        public Person(String Name, int Age)
        {
            name = Name;
            age = Age;
        }
        public static void main(String args[])
        {
            Person myPerson = new Person();
            HashMap myMap = new HashMap();
    
            myMap.put("1", myPerson); //is this valid?
    
            Set set = myMap.entrySet();
            Iterator iter = set.iterator();
    
            while(iter.hasNext())
            {
               Map.Entry hashMap = (Map.Entry)iter.next();
               System.out.println(hashMap.getKey() + ". " + hashMap.getValue());
            }
       }
    }
    My question is: how do I get the data members from the object that is stored as a value in the hash map?

    Thanks
    Last edited by digitol97; 09-12-2010 at 09:27 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you're using Java 1.5 or higher, then you should use a generic HashMap<Person> not a plain HashMap as well as a generic Set and Iterator. Then the object returned by the iterator will be a Person object. Otherwise you'll need to cast the object returned as a Person object.

  3. #3
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    You need to parameterize the HashMap (Generics).
    Java Code:
    HashMap<Integer,Person> myMap = new HashMap<Integer,Person>;
    Then, to get a value:

    Java Code:
    Person p = myMap.get(1);

  4. #4
    digitol97 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    Ah, I got it! Thanks a whole lot. :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Fubarable View Post
    If you're using Java 1.5 or higher, then you should use a generic HashMap<Person> not a plain HashMap ...
    What the "H" was I thinking, HashMap<Person>? To the OP, sorry for the misdirection.

Similar Threads

  1. Something like HashMap
    By BigBear in forum New To Java
    Replies: 4
    Last Post: 03-14-2010, 11:08 AM
  2. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  3. HashMap Help
    By ScaryJello in forum New To Java
    Replies: 2
    Last Post: 04-15-2009, 09:18 AM
  4. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 02:29 PM
  5. hashmap
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 09-24-2008, 12:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •