2010-05-14 128 views
0

我不知道如何使用get()来获取我的信息。看着我的书,他们通过密钥得到()。我认为get()返回与该文档关联的对象。但是我一定在这里做错了什么......任何想法?帮助Java中的哈希表映射

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

你做错了的事情是,你插入以相反的顺序键和值(假设你要寄的关键)。您可以在docs中看到put的签名需要(key, value)

变化

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

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

private Map<Person, String> staff = new HashMap<Person, String>(); 

private Map<String, Person> staff = new HashMap<String, Person>(); 

现在,您将能够通过其电子邮件地址查找Person

+0

他还需要一个更新的地图。 – 2010-05-14 04:10:34

0

重要的是要认识到地图是定向。也就是说,如果您有一个Map<Person, Date>存储生日,那么很容易查看任何人的生日,并且无法查找生日的人(可能很容易出现零或超过一个)。

现在,你想查询一个人的电子邮件地址或电子邮件地址的人吗?您的代码是围绕混合这些事情:

  • 您申报图是Map<Person,String>,建议你将使用人作为关键和字符串作为值 - 即,查找一个人的电子邮件地址。
  • 您使用staff.put(p, p.getEmail())添加数据,这也表明您将使用人员作为关键字。
  • 但是,您尝试定义一个findByEmail方法,该方法必须将电子邮件地址用作关键字 - 这与您设置地图的方式相反。

所以,地图只能朝一个方向走。你可以决定它是哪个方向,但你必须保持一致。如果您需要能够在两个方向上进行查找,请考虑使用两张地图!

+0

请参阅Google Collections中的'BiMap'以获取地图示例,让您通过值(通过地图反演)查找关键字。 – danben 2010-05-14 04:18:00

0

这里有一个片段,显示大部分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'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" 
    } 
}