Reputation: 231
I'm completely new and right now I'm writing a bid of code for university. I want to create an open hashtable and I wrote this peace of code:
public class AuDOpenHashTable extends AuDHashTable {
private LinkedList<Contact>[] table;
public AuDOpenHashTable(int capacity) {
super(capacity);
this.table = new LinkedList[capacity];
}
@Override
public void insert(Contact c) {
int position = hash(c.email);
if (table[position] == null) {
table[position] = new LinkedList<>();
}
table[position].add(c);
}
@Override
public void remove(Contact c) throws NoSuchElementException{
int position = hash(c.email);
if(table[position] != null){
table[position].remove();
}
else{
throw new NoSuchElementException();
}
}
@Override
public Contact getContact(String email)throws NoSuchElementException{
int position = hash(email);
table[position].getContact(email);
if(table[position] != null){
return table[position].get(position);
}
else{
throw new NoSuchElementException();
}
}
}
public abstract class AuDHashTable {
protected int capacity;
public AuDHashTable(int capacity){
this.capacity = capacity;
}
public abstract void insert(Contact c);
public abstract void remove(Contact c);
public abstract Contact getContact(String email);
protected int hash(String s){
int hash = 0;
for(int i = 0; i < s.length(); i++){
hash += s.charAt(i);
}
hash = hash % capacity;
return hash;
}
public static void main(String[] args) {
AuDClosedHashTable hashtabelle = new AuDClosedHashTable(3);
Contact eins = new Contact("[email protected]");
Contact zwei = new Contact("[email protected]");
Contact drei = new Contact("[email protected]");
hashtabelle.insert(eins);
hashtabelle.insert(zwei);
hashtabelle.insert(drei);
System.out.println(hashtabelle.isFull());
System.out.println(hashtabelle.getIndexOf("[email protected]"));
System.out.println(hashtabelle.getIndexOf("[email protected]"));
System.out.println(hashtabelle.getIndexOf("[email protected]"));
hashtabelle.remove(drei);
System.out.println(hashtabelle.isFull());
System.out.println(hashtabelle.getContact("[email protected]"));
System.out.println(hashtabelle.getContact("[email protected]"));
System.out.println(hashtabelle.getContact("[email protected]"));
AuDOpenHashTable hashtabelle = new AuDOpenHashTable(3);
Contact eins = new Contact("[email protected]");
Contact zwei = new Contact("[email protected]");
Contact drei = new Contact("[email protected]");
hashtabelle.insert(eins);
hashtabelle.insert(zwei);
hashtabelle.insert(drei);
System.out.println(hashtabelle.getContact("[email protected]"));
hashtabelle.remove(zwei);
System.out.println(hashtabelle.getContact("[email protected]"));
}
}
So, my problem is in the "getContact()" method. If i want to display an account on a certain position and it is the ONLY account on that position, then everything works fine. But, if want to display an account in which the head differs the tail, so there are two accounts, it only give me one account(mostly not the correct one). For these examples the code works very well, but if i decide to pick other names, sometimes it does also not work. But not to make it complicated I wanted to hear your suggestions on how I can improve the "getContact" method. Thanks in prehand.
Upvotes: 1
Views: 91
Reputation: 11
Different keys can have the same hash code. This is detected at insertion usually in which case there's usually a rehash, some algorithm to produce another hash code, that results in another possibly free has code. If not free it is again rehashed. If this continues a lot then the table was possibly allocated to small and a bigger table should be used.
When retrieving the information, you should compare the data at the index with the key searched. If not matching, rehash ( same algorith as insert ) and try again. Until you find it or end up in an empty index, in which case the key was not there.
Upvotes: 1
Reputation: 18569
The hash function will tell you which bucket an item can be in, but you still need to check all the items within the bucket for equality. getContact
should iterate over the LinkedList and check the email against each contact, then only return the contact with the matching email. Same for the remove
method.
Upvotes: 4