How can I make a hashTable
with three parameters? I want to store phone numbers, names and addresses using a hashTable
and a dictionary
. Phone number as the key, and the name, address as its value. But I can put two data only, phone number and name. How do I get to save a phone number, name, address in the hashTable
?
Hashtable phoneBook;
public FrmPhoneBook()
{
InitializeComponent();
phoneBook = new Hashtable();
}
public void addNewPhoneBook(string name, string tel, string add)
{
string names = name;
string telp = tel;
string address = add;
if (!phoneBook.ContainsKey(telp))
{
phoneBook.Add(telp, names);
getDetails();
}
}
public void getDetails()
{
lvDetails.Items.Clear();
foreach (DictionaryEntry values in phoneBook)
{
lvDetails.Items.Add(values.Value.ToString());
lvDetails.Items[lvDetails.Items.Count - 1].SubItems.Add(
values.Key.ToString());
}
}
You're looking for a Lookup, which can natively store multiple values for each key. As pointed out this only works for a fixed list since you cannot add entries to a lookup once you have created it. Lookup is immutable.
In Hashtable, the Key MUST be unique, as you are NOT using unique keys, the same value is being replaced. so try to put the values with different keys.
Ans. HashTable has been deprecated. As an alternative, ConcurrentHashMap has been provided. It uses multiple buckets to store data and hence much better performance than HashTable.
A Hashtable is an array of a list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
You can have the key as the phone number and the value as a struct which has two members one being the address and one being the name. Also consider moving to Dictionary as it is typesafe
struct User
{
public string Name;
public string Address;
}
static void Main(string[] args)
{
Dictionary<string, User> hash = new Dictionary<string, User>();
//To add to the hash
hash.Add( "22255512282" ,
new User(){ Name = "foo" , Address = "Bar" });
//To lookup by key
User user;
if (hash.TryGetValue("22255512282", out user))
{
Console.WriteLine("Found " + user.Name);
}
}
Put all your user data into a class:
public class User
{
public string Name { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
}
Then process as follows:
Dictionary<string, User> reverseLookUp = new Dictionary<string, User>();
User user;
// Fill dictionary
user = new User { Name = "John", Address = "Baker Street", PhoneNumber = "012345" };
reverseLookUp.Add(user.PhoneNumber, user);
user = new User { Name = "Sue", Address = "Wall Street", PhoneNumber = "333777" };
reverseLookUp.Add(user.PhoneNumber, user);
// Search a user
string phoneNumber = "012345";
if (reverseLookUp.TryGetValue(phoneNumber, out user)) {
Console.WriteLine("{0}, {1}, phone {2}", user.Name, user.Address, user.PhoneNumber);
} else {
Console.WriteLine("User with phone number {0} not found!", phoneNumber);
}
// List all users
foreach (User u in reverseLookUp.Values) {
Console.WriteLine("{0}, {1}, phone {2}", u.Name, u.Address, u.PhoneNumber);
}
You could also create a specialized dictionary for that purpose:
public class PhoneDict : Dictionary<string, User>
{
public void Add(User user)
{
Add(user.PhoneNumber, user);
}
}
Then add users as follows:
PhoneDict phoneDict = new PhoneDict();
User user;
// Fill dictionary
user = new User { Name = "John", Address = "Baker Street", PhoneNumber = "012345" };
phoneDict.Add(user);
user = new User { Name = "Sue", Address = "Wall Street", PhoneNumber = "333777" };
phoneDict.Add(user);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With