Help with Hashmaps in Java
I'm not sure how to use get () to get my information. Looking at my book, they pass the key to get (). I thought 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"));
}
}
a source to share
What you are doing wrong is that you are inserting the key and value in reverse order (assuming you want the letter to be the key). You can see in the docs , the signature to put
take (key, value)
.
Edit
staff.put(p, p.getEmail());
to
staff.put(p.getEmail(), p);
and
private Map<Person, String> staff = new HashMap<Person, String>();
to
private Map<String, Person> staff = new HashMap<String, Person>();
You should now be able to find it Person
by his email address.
a source to share
It is important to understand that maps are directional. That is, if you have Map<Person, Date>
where the birthdays are stored, then it is easy to find someone on the birthday, and it is impossible to find the birthday person (there could easily be zero or more than one).
Now, do you want to find the email address of a person or a person with an email address? Your code mixes these things around:
- You declare the card
Map<Person,String>
, assuming you will use Faces as the key and Strings as the value, that is, looking up the person's email address. - You add data with
staff.put(p, p.getEmail())
, which also assumes that you will be using Faces as the key. - But you are trying to define a method
findByEmail
that will definitely use the email address as the key - from how you set up your card.
Thus, the map can only move in one direction. You decide which direction it is in, but you have to be consistent. If you need to be able to search both ways, consider using two cards!
a source to share
Here's a snippet that shows most of the features Map
:
import java.util.*;
public class MapExample {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map.size()); // prints "3"
System.out.println(map);
// prints "{Three=3, One=1, Two=2}"
// HashMap allows null keys and values
// Map also allows several keys to map to the same values
map.put(null, 1);
map.put("?", null);
System.out.println(map.size()); // prints "5"
System.out.println(map);
// prints "{null=1, Three=3, ?=null, One=1, Two=2}"
// get values mapped by key
System.out.println(map.get("One")); // prints "1"
System.out.println(map.get("Two")); // prints "2"
System.out.println(map.get("Three")); // prints "3"
// get returns null if
// (i) there no such key, or
// (ii) there such key, and it mapped to null
System.out.println(map.get("Four") == null); // prints "true"
System.out.println(map.get("?") == null); // prints "true"
// use containsKey to check if map contains key
System.out.println(map.containsKey("Four")); // prints "false"
System.out.println(map.containsKey("?")); // prints "true"
// use keySet() to get view of keys
Set<String> keys = map.keySet();
System.out.println(keys);
// prints "[null, Three, ?, One, Two]"
// the view supports removal
keys.remove("Three");
System.out.println(map);
// prints "{null=1, ?=null, One=1, Two=2}"
// use values() to get view of values
Collection<Integer> values = map.values();
System.out.println(values);
// prints "[1, null, 1, 2]"
// the view supports removal
values.remove(null);
System.out.println(map);
// prints "{null=1, One=1, Two=2}"
values.remove(1); // removes at most one mapping
System.out.println(map);
// prints "{One=1, Two=2}"
// iterating all entries using for-each
for (Map.Entry<String,Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "->" + entry.getValue());
}
// prints "One->1", "Two->2"
map.clear();
System.out.println(map.isEmpty()); // prints "true"
}
}
a source to share