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", "JW@ucsd.edu");
testObj.add(person1);
System.out.println(testObj.findByEmail("JW@ucsd.edu"));
}
}
The thing you are doing wrong is that you are inserting the key and value in reverse order (assuming you want email to be the key). You can see in the docs that the signature for
puttakes(key, value).Change
to
and
to
Now you will be able to look up a
Personby its email address.