Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   New to Java-Question About HashMap (http://www.velocityreviews.com/forums/t137520-new-to-java-question-about-hashmap.html)

JR 10-09-2004 07:51 PM

New to Java-Question About HashMap
 
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;
}

Sudsy 10-09-2004 08:12 PM

Re: New to Java-Question About HashMap
 
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.


Oscar kind 10-10-2004 07:21 AM

Re: New to Java-Question About HashMap
 
JR <jrolandumuc@yahoo.com> 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

JR 10-10-2004 03:17 PM

Re: New to Java-Question About HashMap
 
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 10-10-2004 03:24 PM

Re: New to Java-Question About HashMap
 
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
*/


All times are GMT. The time now is 05:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.