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