• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Implementing a Hash Table

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright so we have to implement a basic hash table for an assignment. It seems so simple but it's kicking my ass.
Basically what we have is an input file that contains the amount of entries on the first line and a dns record on each subsequent line.
So we are meant to make 3 classes: DNSKey, Record and HashTable.
These are the outlines they've given us



I have been trying to use an array of DNSKey's which are placed into a position decided by the hash function. I am however unsure if I even need an array of DNSKey's or if they should be hashed. So I'm basically wondering how I would map those DNSKey's to a record and placing that record
into the hashtable, while keeping track of the duplicates. The rest of the code basically follows the specification although I am a little unsure on how I would go about the HashTable class.

Here is the code I'm using to do what I explained:



And HashTable class



At the end I basically have to output the DNS key that occured the most and the amount of times it occured.

Thanks a ton if anyone can help/\.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Molloy wrote:
I have been trying to use an array of DNSKey's which are placed into a position decided by the hash function. I am however unsure if I even need an array of DNSKey's or if they should be hashed. So I'm basically wondering how I would map those DNSKey's to a record and placing that record
into the hashtable, while keeping track of the duplicates. The rest of the code basically follows the specification although I am a little unsure on how I would go about the HashTable class.



Maintaining an array to store the elements in your HashTable class would be good choice. Alternatively you could use a Collection that implements the RandomAccess interface, of which ArrayList would be most suitable. I'd go for an array, though. A general purpose HashTable would likely use an inner class that represents an entry in the HashTable that wraps both the key and the value, however, if all you're going to store are Record objects that does not seem necessary as: "For this assignment key is simply stored in the Record". So your HashTable can simply maintain an array of Record objects, right? Well, if you could be absolutely sure that the hashing algorithm would always return a unique value, then yes, however the algorithm description is as follows: "use the sum of ASCII values for all characters in domain_name and source_address. Caller must reduce h modulo the hash table size". So there's quite a good chance of duplicate hash code's for different values. Such an occurence is called a "collision". That means keeping track of an array of Record objects directly is not good enough, as a Record in the array would be overwriten by adding another Record wo's key happens to hash to the same index position, but which is otherwise unequal. One of the simplest conceptual ways to deal with this problem would be to maintain an array of an array of Record objects. That way a key's hash value would always represent an array of Record objects, which means you could keep track of more than one Record for a given hash value. That nested array would represent what's known as a "bucket". However, implementing a bucket using an array is not really ideal, because an array's size is not dynamic. A linked list would probably be more efficient bucket implementation. See if you can work out a solution like this.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic