New to Java-Question About HashMap

Discussion in 'Java' started by JR, Oct 9, 2004.

  1. JR

    JR Guest

    Hi. I'm new to Java and have a question about how Java hashes work.
    They are a bit different than the other languages to which I am
    accustomed. I've borrowed the following code examples from various
    websites and I'm just curious if there is a way to get at the
    firstName and lastName methods of the Person class from the object
    that is stored in the HashMap in the following examples. For
    example,"System.out.println(Value);" will print the objects in the
    HashMap, but if I want to get at the firstName and lastName methods of
    the Person class, can I get to them from within the object that is
    stored in the HashMap? I know I can get to them directly through the
    Person class, but I'm just wondering if it's possible to also get at
    them through the HashMap. I apologize if this is a stupid question,
    but I'm new to Java and couldn't find the answers online anywhere,
    after much searching. I've tried code such as
    System.out.println(Value.getFirstName()) and some other things, but
    what I'm trying to do may not be possible, as far as I know. I'm just
    curious..

    Thanks.

    System.out.println(

    import java.io.*;
    import java.util.*;
    import java.util.Enumeration;

    public class HashCodeExample2 {

    public static void hashMapExample() {

    // Create new hashmap
    HashMap map = new HashMap();

    // Create 3 new person objs (can't say objects in comments?!)

    Person p1 = new Person("J1", "Z1");
    Person p2 = new Person("J2", "Z2");
    Person p3 = new Person("J3", "Z3");

    // Store objects in hash map
    map.put("J1", p1);
    map.put("J2", p2);
    map.put("J3", p3.getFirstName()+" "+p3.getLastName());

    // Iterate over the keys in the map
    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
    // Get key
    Object Key = it.next();
    System.out.println(Key);
    }

    // Iterate over the values in the map
    it = map.values().iterator();
    while (it.hasNext()) {
    // Get value
    Object Value = it.next();
    System.out.println(Value);
    }
    }

    public static void main(String[] args) { hashMapExample(); }
    }




    ################

    import java.io.*;
    import java.util.*;

    public class Person {

    // Constructor
    public Person(String firstName, String lastName) {

    // Use of "this" differentiates the instance variables
    // for this class from the string object passed into
    // the constructor.
    this.firstName = firstName;
    this.lastName = lastName;

    }

    // Get methods
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }

    // Set methods
    public String setFirstName(String FirstName) {
    if (!FirstName.equals("")) firstName = FirstName;
    return firstName;
    }

    public String setLastName(String LastName) {
    if (!LastName.equals("")) lastName = lastName;
    return lastName;
    }

    String firstName, lastName;
    }
    JR, Oct 9, 2004
    #1
    1. Advertising

  2. JR

    Sudsy Guest

    JR wrote:
    <snip>
    > // Iterate over the keys in the map
    > Iterator it = map.keySet().iterator();
    > while (it.hasNext()) {
    > // Get key
    > Object Key = it.next();
    > System.out.println(Key);
    > }
    >
    > // Iterate over the values in the map
    > it = map.values().iterator();
    > while (it.hasNext()) {
    > // Get value
    > Object Value = it.next();
    > System.out.println(Value);
    > }


    Mormal way is to itertate over the keys and cast the gets to
    an object reference of appropriate type. Something like this:

    Iterator it = map.keySet().iterator;
    while( it.hasNext() ) {
    Object key = it.next();
    Person value = (Person) map.get( key );
    }

    Make sense?

    --
    Java/J2EE/UNIX consulting and remote development.
    Sudsy, Oct 9, 2004
    #2
    1. Advertising

  3. JR

    Oscar kind Guest

    JR <> wrote:
    > import java.io.*;
    > import java.util.*;
    > import java.util.Enumeration;
    >
    > public class HashCodeExample2 {
    >
    > public static void hashMapExample() {
    >
    > // Create new hashmap
    > HashMap map = new HashMap();
    >
    > // Create 3 new person objs (can't say objects in comments?!)
    >
    > Person p1 = new Person("J1", "Z1");
    > Person p2 = new Person("J2", "Z2");
    > Person p3 = new Person("J3", "Z3");
    >
    > // Store objects in hash map
    > map.put("J1", p1);
    > map.put("J2", p2);
    > map.put("J3", p3.getFirstName()+" "+p3.getLastName());


    Don't do this: if you mix Person and String objects in the Map, you don't
    know what to cast the objects to (as Map.get(String) returns an Object).

    To avoid the use of the "instanceof" operator (always a good idea), use
    only objects of the same class in the Map. Or objects of different classes
    that all have the same superclass or implement the same interface (and
    cast to that).

    >
    > // Iterate over the keys in the map
    > Iterator it = map.keySet().iterator();
    > while (it.hasNext()) {
    > // Get key
    > Object Key = it.next();
    > System.out.println(Key);
    > }
    >
    > // Iterate over the values in the map
    > it = map.values().iterator();
    > while (it.hasNext()) {
    > // Get value
    > Object Value = it.next();
    > System.out.println(Value);
    > }
    > }
    >
    > public static void main(String[] args) { hashMapExample(); }
    > }
    >
    >
    >
    >
    > ################
    >
    > import java.io.*;
    > import java.util.*;
    >
    > public class Person {
    >
    > // Constructor
    > public Person(String firstName, String lastName) {
    >
    > // Use of "this" differentiates the instance variables
    > // for this class from the string object passed into
    > // the constructor.
    > this.firstName = firstName;
    > this.lastName = lastName;
    >
    > }
    >
    > // Get methods
    > public String getFirstName() { return firstName; }
    > public String getLastName() { return lastName; }
    >
    > // Set methods
    > public String setFirstName(String FirstName) {
    > if (!FirstName.equals("")) firstName = FirstName;
    > return firstName;
    > }
    >
    > public String setLastName(String LastName) {
    > if (!LastName.equals("")) lastName = lastName;
    > return lastName;
    > }
    >
    > String firstName, lastName;
    > }


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Oct 10, 2004
    #3
  4. JR

    JR Guest

    Thanks Sudsy and Oscar. I realize now not to mix Person and String
    objects in the same class, and how to extract a given object's
    method(s) from within a HashMap.

    Thanks again!

    JR

    public class HashCodeExample2 {

    public static void hashMapExample() {

    // Create new hashmap
    HashMap map = new HashMap();

    // Create 3 new person objs (can't say objects in comments?!)

    Person p1 = new Person("J1", "Z1");
    Person p2 = new Person("J2", "Z2");
    Person p3 = new Person("J3", "Z3");

    // Store objects in hash map
    map.put("JR", p1);
    map.put("DZ", p2);
    map.put("HR", p3);

    Iterator it = map.keySet().iterator();
    while( it.hasNext() ) {
    Object key = it.next();
    Person value = (Person)map.get(key);

    // Object's toString() representation
    System.out.println(value);

    // Object's getFirstName method toString() representation
    System.out.println(value.getFirstName());

    // Object's getLastName method toString() representation
    System.out.println(value.getLastName());
    }
    }

    public static void main(String[] args) { hashMapExample(); }
    }
    JR, Oct 10, 2004
    #4
  5. JR

    JR Guest

    Oops, I included the wrong code in the last response. Below is the
    changed code that complied and returned the expected result (plus a
    test of the remove method). Thanks again for the help.

    import java.io.*;
    import java.util.*;
    import java.util.Enumeration;

    public class HashCodeExample2 {

    public static void hashMapExample() {

    // Create new hashmap
    HashMap map = new HashMap();

    // Create 3 new person objs
    Person p1 = new Person("J1", "Z1");
    Person p2 = new Person("J2", "Z2");
    Person p3 = new Person("J3", "Z3");

    // Store objects in hash map
    map.put("J1", p1);
    map.put("J2", p2);
    map.put("J3", p3);

    // Remove first key
    map.remove("J1");

    Iterator it = map.keySet().iterator();
    while( it.hasNext() ) {
    Object key = it.next();
    Person value = (Person)map.get(key);

    // Object's toString() representation
    System.out.println(value);

    // Object's getFirstName method toString() representation
    System.out.println(value.getFirstName());

    // Object's getLastName method toString() representation
    System.out.println(value.getLastName());
    }
    }

    public static void main(String[] args) { hashMapExample(); }
    }

    /* Output
    Person@17943a4
    J3
    Z3
    Person@480457
    J2
    Z2
    */
    JR, Oct 10, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince Darley
    Replies:
    4
    Views:
    4,368
    emilchacko
    Mar 2, 2010
  2. Ian Pilcher

    new HashMap(mapSize, 1)?

    Ian Pilcher, Aug 22, 2005, in forum: Java
    Replies:
    4
    Views:
    941
    John C. Bollinger
    Aug 23, 2005
  3. hrpreet

    Java HashMap Size

    hrpreet, Nov 24, 2005, in forum: Java
    Replies:
    2
    Views:
    19,968
    Thomas Hawtin
    Nov 24, 2005
  4. www
    Replies:
    30
    Views:
    2,971
    Adam Maass
    Oct 23, 2007
  5. Rakesh
    Replies:
    10
    Views:
    12,131
    Mike Schilling
    Apr 8, 2008
Loading...

Share This Page