2010-05-14 142 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" 
    } 
}