javahashtablephonebook

How to associate multiple values inside a hashtable - java


I am trying to implement a basic phonebook using a hashtable made from scratch i made but when adding a contact i will need to store atleast 2 informations, the name and number of each person.

The problem is when adding the info into the hashtable i can only do it like x.insert(name) and x.insert(number) witch will result in 2 different keys and i cant find away to associate the two values within the hashtable. Is this even possible to do?

If needed i can provide the code.

PS: the hashtable i made has the methods: insert(y),remove(y),find(y),print()

Thanks in advance.


Solution

  • as said by @hnefatl in a comment, create some class:

    public class PhoneBookInfos {
        public String Name;
        public String Number;
    }
    

    and your hashtable/HashMap would be:

    Map<Integer, PhoneBookInfos> myPhoneBook = new HashMap<Integer, PhoneBookInfos>();
    

    updated after @hnefatl's comment