Java中Hashmaps的帮助

Java中Hashmaps的帮助,java,hashmap,Java,Hashmap,我不知道如何使用get获取我的信息。看着我的书,他们把钥匙递给我。我认为get会返回与该键相关联的对象,查看文档。但我一定是做错了什么。。。。有什么想法吗 import java.util.*; public class OrganizeThis { /** Add a person to the organizer @param p A person object */ public void add(Person p) {

我不知道如何使用get获取我的信息。看着我的书,他们把钥匙递给我。我认为get会返回与该键相关联的对象,查看文档。但我一定是做错了什么。。。。有什么想法吗

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"));
    }
}

你做错了的事情是,假设你想让电子邮件成为密钥,那么你正在以相反的顺序插入密钥和值。您可以在中看到put的签名采用key,value

改变

staff.put(p, p.getEmail());


现在,您可以通过电子邮件地址查找某人。

认识到地图的方向性很重要。也就是说,如果你有一张存储生日的地图,那么很容易查找任何人的生日,而不可能查找一个生日的人,因为很可能是零个,或者多个

现在,您希望能够查找一个人的电子邮件地址,还是一个电子邮件地址的人?您的代码混合了以下内容:

您将map声明为map,这意味着您将使用Persons作为键,使用string作为值——也就是说,查找一个人的电子邮件地址。 您可以使用staff.putp、p.getEmail添加数据,这也表明您将使用Persons作为键。 但是您尝试定义一个findByEmail方法,该方法必须使用电子邮件地址作为键-这与您设置地图的方式相反。
因此,地图只能朝一个方向移动。你可以决定它的方向,但你必须始终如一。如果你需要能够在两个方向做查找,考虑使用两张地图! 以下是一个显示大多数地图功能的片段:

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"
    }
}
请参阅Google Collections中的BiMap,以获取一个地图示例,该地图允许您通过地图反转按值查找键。
private Map<Person, String> staff = new HashMap<Person, String>();
private Map<String, Person> staff = new HashMap<String, Person>();
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"
    }
}