2016-07-27 138 views
1

存儲電話簿聯繫人的最佳數據結構是什麼?每個數據結構都由名字,姓氏和電話號碼組成。用戶必須能夠通過每個字段進行搜索。 也有類似的問題,但答案都不夠清楚。用於存儲電話簿數據的數據結構

+0

我會說使用哈希表,但它真的是你舒服的。 – Michael

+0

所有的電話號碼都是美國的還是會有那些奇怪格式的非美國號碼? –

回答

2

創建一個POJO類型,它存儲名字,姓氏和電話號碼(如果需要可以使其變爲可變的)。

class PhoneBookEntry { 
    public final String firstName; 
    public final String lastName; 
    public final String phoneNumber; 

    public Entry(String firstName, String lastName, String phoneNumber) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.phoneNumber = phoneNumber; 
    } 

    //... equals + hashcode implementation 
} 

你可以這樣創建您的電話簿:

class PhoneBook { 
    private Map<String, Set<PhoneBookEntry>> firstNameMap; 
    private Map<String, Set<PhoneBookEntry>> lastNameMap; 
    private Map<String, Set<PhoneBookEntry>> phoneNumberMap; 

    public void add(PhoneBookEntry entry) { 
     Set<PhoneBookEntry> set 
      = firstNameMap.computeIfAbsent(entry.firstName, k -> new HashSet<>()); 
     set.add(entry); 

     set = lastNameMap.computeIfAbsent(entry.lastName, k -> new HashSet<>()); 
     set.add(entry); 

     set = phoneNumberMap.computeIfAbsent(entry.phoneNumber, k -> new HashSet<>()); 
     set.add(entry); 
    } 

    public Set<PhoneBookEntry> getByFirstName(String firstName) { 
     return firstNameMap.get(firstName); 
    } 

    public Set<PhoneBookEntry> getByLastName(String lastName) { 
     return lastNameMap.get(lastName); 
    } 

    public Set<PhoneBookEntry> getByPhoneNumber(String phoneNumber) { 
     return phoneNumberMap.get(phoneNumber); 
    } 

} 

使用Map允許的數量快速查找。

正如yitzih所說,多個聯繫人可以有相同的名字,姓氏或電話號碼。因此,按名字查找(例如)將返回一組聯繫人。

1

創建一個存儲每個聯繫人所需變量的聯繫人對象。使用ArrayList來存儲它們。

沒有關於聯繫人的更多信息,實際上沒有辦法使用HashTable,Map或Graph。 HashTable沒有真正的鍵值對,除非你想使用名字和姓氏的組合,但是你需要一些方法來處理衝突(如果2個人有完全相同的名字),或者你需要禁止有兩個人有相同的聯繫人名稱(但你爲什麼要這麼做?)

+0

如果我爲每個聯繫人分配一個唯一的ID用作密鑰,我如何存儲其餘數據?價值是什麼? – Mitaryss

+0

您將使用HashMap/HashTable/TreeMap,其中Key是int或String或任何組成唯一標識的數據類型,並且值是Contact對象。但是,這意味着你需要知道唯一的ID從地圖中檢索對象。 – yitzih

0
Class Contact{ 

String forename; 
String Surname; 
String phoneNo; 

public Contact(fName, sName, pNo){ 
forename = fName; 
Surname = sName; 
phoneNo = pNo; 
} 

public String getForename(){} 

public String getSurname(){} 

public String getPhoneNo(){} 

}

班上處理搜索

, 聲明類型聯繫的ArrayList,以及搜索聯繫人時說約翰,一個模糊的問題

public Contact searchContact(String s){ 
for(int i = 0; i< ContactList.size(); i++){ 
if(ContactList.get(i).getForename().equals(s) || 
       ContactList.get(i).getSurame().equals(s) || 
      ContactList.get(i).getPhoneNo().equals(s) 
){ 
return ContactList.get(i); 
} 
} 

return null; 
} 
0

類,但到底是什麼,也許這會消除我午飯後的嗜睡症。我假設一個簡單的電話號碼字符串表示,但最好的數據對象來存儲世界電話號碼的所有可能的品種以及智能搜索它們的方法(例如,「(123)456-7891」與「1234567891」?)可能完全是它自己的問題。

這裏的PhoneBook類存儲所有的聯繫人。方法searchFirst(),searchLast()和searchPhoneNumber()都返回匹配聯繫人列表。

public class PhoneBook { 

    ArrayList<Contact> contacts; 

    public PhoneBook() { 
     contacts = new ArrayList<>(); 
    } 

    public void addContact(Contact contact) { 
     contacts.add(contact); 
    } 

    public ArrayList<Contact> searchFirst(String first) { 
     ArrayList<Contact> foundContacts = new ArrayList<>(); 
     for (Contact contact: contacts) { 
      if (contact.first.equals(first)) { 
       foundContacts.add(contact); 
      } 
     } 
     return foundContacts; 
    } 

    public ArrayList<Contact> searchLast(String last) { 
     ArrayList<Contact> foundContacts = new ArrayList<>(); 
     for (Contact contact: contacts) { 
      if (contact.last.equals(last)) { 
       foundContacts.add(contact); 
      } 
     } 
     return foundContacts; 
    } 

    public ArrayList<Contact> searchPhoneNumber(String phoneNumber) { 
     ArrayList<Contact> foundContacts = new ArrayList<>(); 
     for (Contact contact: contacts) { 
      if (contact.phoneNumber.equals(phoneNumber)) { 
       foundContacts.add(contact); 
      } 
     } 
     return foundContacts; 
    } 

    class Contact { 
     String first; 
     String last; 
     String phoneNumber; 

     public Contact(String first, String last, String phoneNumber) { 
      this.first = first; 
      this.last = last; 
      this.phoneNumber = phoneNumber; 
     } 
    } 

}