简体   繁体   中英

Data structure for storing phonebook data

What would be the best data structure to store phone book contacts, each consisting of first name, last name and phone number. The user must be able to search by each one of the fields. There has been similar questions, but none of the answers were clear enough.

Create a POJO type, that stores first name, last name, and phone number (could make it mutable if needed).

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
}

You can create your phone book like this:

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);
    }

}

Using Map s allows for fast lookup.

As yitzih said, Multiple contacts can have the same first name, last name, or phone number. So a lookup by first name (for instance), will return a set of contacts.

Create a Contact object that stores the variables needed for each contact. Use an ArrayList to store them.

Without having more information about the contact there isn't really any way to use a HashTable, Map or Graph. There is no real key value pair for a HashTable unless you want to use a combination of first and last names, but you would need some way to handle conflicts (if 2 people have the exact same name.), or you would need to forbid having 2 people having the same Contact name (but why would you want to do that?)

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(){}

}

in the class handling the search, you declare an arrayList of type Contact, and when searching for a contact say John,

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;
}

Kind of a vague question, but what the heck, maybe this'll chase away my post-lunch sleepies. I'm assuming a simple String representation of the phone number, but the best data object to store all the possible varieties of world phone numbers along with a method to intelligently search them (eg is "(123) 456-7891" the same as "1234567891"?) could be it's own question entirely.

Here a PhoneBook class stores all of the contacts. The methods searchFirst(), searchLast() and searchPhoneNumber() each return lists of matching contacts.

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;
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM