Help with Hashmaps in Java
Posted
by Crystal
on Stack Overflow
See other posts from Stack Overflow
or by Crystal
Published on 2010-05-14T04:06:41Z
Indexed on
2010/05/14
4:14 UTC
Read the original article
Hit count: 283
I'm not sure how I use get() to get my information. Looking at my book, they pass the key to get(). I thought that get() returns the object associated with that key looking at the documentation. But I must be doing something wrong here.... Any thoughts?
import java.util.*;
public class OrganizeThis
{
/**
Add a person to the organizer
@param p A person object
*/
public void add(Person p)
{
staff.put(p, p.getEmail());
System.out.println("Person " + p + "added");
}
/**
* Find the person stored in the organizer with the email address.
* Note, each person will have a unique email address.
*
* @param email The person email address you are looking for.
*
*/
public Person findByEmail(String email)
{
Person aPerson = staff.get(email);
return aPerson;
}
private Map<Person, String> staff = new HashMap<Person, String>();
public static void main(String[] args)
{
OrganizeThis testObj = new OrganizeThis();
Person person1 = new Person("J", "W", "111-222-3333", "[email protected]");
testObj.add(person1);
System.out.println(testObj.findByEmail("[email protected]"));
}
}
© Stack Overflow or respective owner