2016-12-10 39 views
0

假設我有一個Person類並且創建了該類的一些對象。然後我製作一張地圖,其中包含人爲關鍵字和字符串作爲值。我想使這需要一個名稱,並查找與該名稱的人,然後打印出關鍵其中有該名稱當地圖中的鍵是一個對象時,如何通過該對象的屬性進行搜索?

這裏的值的方法是我的代碼:

import java.util.HashMap; 
import java.util.Map; 

public class Ex03{ 

public static void main(String[] args) { 

    new Ex03(); 

} 

public Ex03(){ 

    Person person1 = new Person("Name1", "LastName1", "Address1"); 
    Person person2 = new Person("Name2", "LastName2", "Address2"); 
    Person person3 = new Person("Name3", "LastName3", "Address3"); 
    Person person4 = new Person("Name4", "LastName4", "Address4"); 

    Map<Person, String> map = new HashMap<>(); 
    map.put(person1, "12345678901"); 
    map.put(person2, "12345678902"); 
    map.put(person3, "12345678903"); 
    map.put(person4, "12345678904"); 

    findNumber(map, "Name1"); 

} 

public void findNumber(Map<Person, String> map, String name){ 

} 


class Person { 
    String name, lastName, address; 

    public Person(){ 

    } 

    public Person(String name, String lastName, String address){ 

     this.name = name; 
     this.lastName = lastName; 
     this.address = address; 

    } 

    @Override 
    public String toString() { 
     return "Person [name=" + name + ", lastName=" + lastName + ", address=" + address + "]"; 
    } 



} //end of class Person 

} 

我如何使方法findNumber()? 概述:輸入一個字符串名稱,方法找到具有該名稱的Person對象,輸出綁定到具有我們輸入名稱的Person Key的值。在此先感謝

回答

0

除非將名稱的單獨映射存儲到Person對象,否則必須遍歷映射的keySet,將每個鍵與字符串進行比較。

0

然後我製作一張地圖,其中保存人爲關鍵字和字符串作爲值。我 想讓它需要一個名稱,並查找與 該名稱的人,然後打印出關鍵的它有這樣的名字

您的要求兩者是矛盾的值的方法,所以使它更簡單一些,你可以在HashMap中實際存儲關鍵字name的值爲Person對象和值作爲關鍵字(即,但是你正在做倒序)。

選項(1):在HashMap的名作爲關鍵字和人的價值

這就是一般我們做即映射對象如String,整型等

Map<String,Person> map = new HashMap<>(); 
map.put("John", person1); 
//add other objects 
一個簡單的鍵

現在來看看下面findNumber()

public void findNumber(Map<Person, String> map, String name){ 
    Person person = map.get(name); 
} 

選擇(2):人作爲重點和名稱在HashMap中

Map<Person, String> map = new HashMap<>(); 
map.put(person1, "John"); 
//add other objects 

中的價值現在來看看下面findNumber()

public void findNumber(Map<Person, String> map, String name){ 
     Set<Person> keys = map.keySet(); 
     for(Person person : keys) { 
      if(person.getName().equals(name)) { 
       String name = map.get(person); 
       break; 
      } 
     } 
} 

對於此選項(2)工作,你需要覆蓋equals()hashcode()Person類中。

此外,作爲一個側面說明,請記住,構造都是爲了實例變量的初始化和你寫太多邏輯的內部構造你的代碼(如Ex03()),這是不是最好的做法裏面。

0

更改您的代碼如下...

public Ex03(){ 

    Person person1 = new Person("Name1", "LastName1", "Address1"); 
    Person person2 = new Person("Name2", "LastName2", "Address2"); 
    Person person3 = new Person("Name3", "LastName3", "Address3"); 
    Person person4 = new Person("Name4", "LastName4", "Address4"); 

    Map<String, Person> map = new HashMap<>(); 
    map.put("Name1", person1); // or map.put(person1.name, person1); 
    map.put("Name2", person2); 
    map.put("Name3", person3); 
    map.put("Name4", person4); 

    findPerson(map, "Name1"); 

} 

public Person findPerson(Map<String, Person> map, String name){ 
    Person person = map.get(name); 
} 

編輯:

如果你想存儲的個人數,那麼你應該在你的個人類,如下添加。

public Ex03(){ 

    Person person1 = new Person("Name1", "LastName1", "Address1", "12345678901"); 
    Person person2 = new Person("Name2", "LastName2", "Address2", "12345678902"); 
    Person person3 = new Person("Name3", "LastName3", "Address3", "12345678903"); 
    Person person4 = new Person("Name4", "LastName4", "Address4", "12345678904"); 

    Map<String, Person> map = new HashMap<>(); 
    map.put("Name1", person1); // or map.put(person1.name, person1); 
    map.put("Name2", person2); 
    map.put("Name3", person3); 
    map.put("Name4", person4); 

    String number = findNumber(map, "Name1"); 

} 

public String findNumber(Map<String, Person> map, String name){ 
    String number = map.get(name).number; 
} 

class Person { 
    String name, lastName, address, number; 

    public Person(){ 

    } 

    public Person(String name, String lastName, String address, String number){ 

     this.name = name; 
     this.lastName = lastName; 
     this.address = address; 
     this.number = number; 
    } 

    @Override 
    public String toString() { 
     return "Person [name=" + name + ", lastName=" + lastName + ", address=" + address + ", number=" + number"]"; 
    }  

} //end of class Person 
相關問題