New to Java-Question About HashMap

J

JR

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;
}
 
S

Sudsy

JR wrote:
// 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?
 
O

Oscar kind

JR said:
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;
}
 
J

JR

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(); }
}
 
J

JR

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
*/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top