tags:

views:

90

answers:

4

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]"));
    }
}
+7  A: 

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 put takes (key, value).

Change

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>();

Now you will be able to look up a Person by its email address.

danben
He'll also need an update to the Map.
bkail
A: 

It's important to realize that maps are directional. That is, if you have a Map<Person, Date> storing birthdays, then it's easy to look up any person's birthday, and impossible to look up a birthday's person (there could easily be zero, or more than one).

Now, do you want to be able to look up the email address of a person, or the person of an email address? Your code is mixing these things around:

  • You declare the map to be Map<Person,String>, suggesting that you'll be using Persons as the key and Strings as the value -- that is, looking up a person's email address.
  • You add data with staff.put(p, p.getEmail()), which also suggests that you'll use the Persons as the key.
  • But you try to define a findByEmail method, which would necessarily use the email address as the key -- that's backwards from how you've got your map set up.

So, a map can only go in one direction. You get to decide which direction it is, but you have to be consistent. If you need to be able to do lookups in both directions, consider using two maps!

Etaoin
See `BiMap` in Google Collections for an example of a map that lets you look up a key by value (via an inversion of the map).
danben
A: 

A very similar question was asked not long ago which may shed some light on the situation http://stackoverflow.com/questions/2831444/faster-way-to-find-out-the-key-for-given-value-from-map

Jacob
That is really not what the OP is asking; he or she just wanted to know why the code in question was not working as expected.
danben
A: 

Here's a snippet that shows most of the Map functionalities:

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's no such key, or
        //   (ii) there's such key, and it's 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"
    }
}
polygenelubricants