假設我有一個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的值。在此先感謝