2015-11-16 66 views
0

我將一個聯繫人列表存儲爲類型爲「Person」的HashMap,並希望具有搜索功能,以便我可以搜索HashMap並返回名爲「John」的所有人以及例如,住在美國的人。我的想法是隻要創建人與環的ArrayList通過增加每個值作爲例如:在多個值中搜索一個HashMap

Map<Person, Person> hm = new HashMap<Person, Person>(); 
    ArrayList<String> result = new ArrayList<String>(); 

    Enumeration num= hm.keys(); 
    String name = "John"; 
    String location = "USA"; 

    while (num.hasMoreElements()) { 
     Person person = (Person) num.nextElement(); 

     if(person.name.equals(name) && person.location.equals(location)) 
     { 
      result.add(person); 
     } 

我只是想知道這是否會工作正確的,或者如果有,我已經忽略了這樣的一些更好的方法。

感謝

+1

您正在使用'Map ',但看起來您確實應該使用'Set '。您可以閱讀更多:http://java67.blogspot.com.au/2013/01/difference-between-set-list-and-map-in-java.html –

回答

0

而不是使用Enumerable,我會建議你使用for語法上的按鍵。

for (Person person : hm.keys()) 
    { 
     // Your if statement goes here 
    } 
0

你真的想:

Set<Person> hm = new HashSet<Person>(); 
for(Person person: hm) 
{ 
    // your logic here 
} 

如果由於某種原因你還是死心塌地地在地圖上,遍歷這樣的:

for(Map.entry<Person, Person> entry: hm.entrySet()) 
{ 
    // use entry.getKey() and entry.getValue() 
} 
0

有沒有更好的結構解決方案,因爲HashMap以任意的,不透明的順序包含其密鑰,任何算法都不能使用該順序,這些算法不完全知道內部信息。因此,遍歷所有元素(鍵)時沒有乾淨的方法。

我也建議的文體改進已經顯示爲@WW。

0

除非你確實需要映射Person對象我會建議你使用一個Set,而不是Map

Set<Person> people = new HashSet<Person>(); 

的Java 8讓你創建一個過濾集的一個很好的方式:

Set<Person> subset = people.stream() 
    .filter(p -> p.getName().equals(name)) 
    .filter(p -> p.getLocation().equals(location)) 
    .collect(Collectors.toSet()); 

如果你想要某些預定義的搜索條件,那麼你可以創建這些方法:

class Person { 
    public static Predicate<Person> hasNameAndLocation(String name, Location location) { 
     return person -> person.name.equals(name) && person.location.equals(location); 
    } 
} 

,使您的過濾代碼更漂亮,並避免使用干將:

.filter(Person.hasNameAndLocation("Fred", Country.USA)) 

如果需要非常高的性能(可能只需要數以百萬計的搜索第二的項目或上千),那麼解決的辦法是有獨立的地圖,使預定的搜索速度非常快:

Map<String, Set<Person>> nameMap; 
Map<Location, Set<Person>> locationMap; 

Set<Person> subset = nameMap.get("Fred") 
    .filter(locationMap.get(Country.USA)::contains)) 
    .collect(Collectors.toSet()); 

這可能是非常快的,但讓你有多個集合,以跟上你的代碼要複雜得多。除非您有顯着的性能要求,否則不要這樣做。