SlideShare a Scribd company logo
Complete code in Java
The hashtable you'll be making will use Strings as the keys and Object as the values. Similar to
linked lists, by storing Object as values, you can store any kind of object in the hashtable.
package Dictionary;
import java.util.ArrayList;
import java.util.Hashtable;
import List.ListInterface;
import List.MyLinkedList;
public class MyHashtable implements DictionaryInterface {
protected int tableSize;
protected int size;
// The LinkedList is of type Entry
protected MyLinkedList[] table;
public MyHashtable(int tableSize) {
table = new MyLinkedList[tableSize];
this.tableSize = tableSize;
}
public int biggestBucket()
{
int biggestBucket = 0;
for(int i = 0; i < table.length; i++) {
// Loop through the table looking for non-null locations.
if (table[i] != null) {
// If you find a non-null location, compare the bucket size against the largest
// bucket size found so far. If the current bucket size is bigger, set biggestBucket
// to this new size.
MyLinkedList bucket = table[i];
if (biggestBucket < bucket.size())
biggestBucket = bucket.size();
}
}
return biggestBucket; // Return the size of the biggest bucket found.
}
// Returns the average bucket length. Gives a sense of how many collisions are happening
overall.
public float averageBucket() {
float bucketCount = 0; // Number of buckets (non-null table locations)
float bucketSizeSum = 0; // Sum of the size of all buckets
for(int i = 0; i < table.length; i++) {
// Loop through the table
if (table[i] != null) {
// For a non-null location, increment the bucketCount and add to the bucketSizeSum
MyLinkedList bucket = table[i];
bucketSizeSum += bucket.size();
bucketCount++;
}
}
// Divide bucketSizeSum by the number of buckets to get an average bucket length.
return bucketSizeSum/bucketCount;
}
public String toString()
{
String s = "";
for(int tableIndex = 0; tableIndex < tableSize; tableIndex++) {
if (table[tableIndex] != null) {
MyLinkedList bucket = table[tableIndex];
for(int listIndex = 0; listIndex < bucket.size(); listIndex++) {
Entry e = (Entry)bucket.get(listIndex);
s = s + "key: " + e.key + ", value: " + e.value + "n";
}
}
}
return s;
}
protected class Entry
{
String key;
Object value;
Entry(String key, Object value) {
this.key = key;
this.value = value;
}
}
// Implement these methods
public boolean isEmpty() {return true;} // returns true if the Dictionary is empty, false otherwise.
public int size(){return -1;} //Returns the number of key/value pairs stored in the dictionary.
// Adds a value stored under the given key. If the key has already been stored in the Dictionary,
// replaces the value associated with the key and returns the old value. If the key isn't in the
dictionary
// returns null.
public Object put(String key, Object value){
// 1. Compute an array index given the key
// 2. If that location in the table is null,
// that means nothing has been previously stored using a key with this hash code.
// a. Create a new MyLinkedList to be the bucket.
// b. Add the new Entry for the key/value pair to the list.
// c. Set this location in the array equal to the new bucket (list).
// d. Increment the size (the number of unique keys you have stored).
// 3. If the location in the table isn't null,
// that means keys with this colliding hash code have been previously stored.
// 3a, a value exists for the key
// a. Linearly search through the bucket (the list) stored at this array
// location comparing the key for each entry with the key passed into put().
// If you get a match, this means this key as been previously stored.
// Save the old value in the Entry (so you can return it) and replace it with the new value.
// (use the code below)
// Entry oldValue = new Entry(key, e.value);
// e.value = value;
// NOTE: this is technically not correct as you would need to create a deep copy of the entry,
// however, that is outside the realm of this assignment. The code above will be
// enough to complete the assignment
// return old value here
// return oldValue.value;
// 3b, a value does not exist for the key
// b. If you don't find the key in the bucket,
// then just add a new Entry (with the key and value) to the beginning of the list.
// Increment the size.
return null;
}
public Object get(String key){
// 1. Compute an array index given the key
// 2. If that location in the table is null,
// that means nothing has been stored using a key with this hash code.
// So we can return null.
// 4. Linearly search through the bucket (the list),
// comparing the key for each entry with the key passed into get().
// Extracting each element in
// the Linked List
// If you find a match, return the value.
return null;
} // Retrieves the value stored with the key.
public void remove(String key){
// 1. Compute an array index given the key
// 2. If that location in the table is null, then this key has definitely not been used to store a value.
// 3. If the location in the table has a bucket,
// we need to linearly search it to see if it contains an Entry with the key.
// If you find an Entry in the bucket (linked list) with the key:
// a. Remove this Entry from the bucket.
// b. Decrement size (the number of unique keys stored in the hashtable).
} // Deletes the key/value pair stored with the given key.
public void clear(){} // Empties the dictionary.
public String[] getKeys(){
// 1. Create a String[] with a size equal to the number of unique keys in the hashtable
// 2. Iterate through the hashtable array.
// For each table location that isn't null
// a. Iterate though the bucket (linked list)
// getting the key out of each Entry and storing it in
// the array of strings you created in step 1.
// 3. Return the String[]
return null;
}
}

More Related Content

PDF
The hashtable youll be making will use Strings as the keys and Obje.pdf
PDF
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
PDF
using the code below write the public V add(K key, V value); that ad.pdf
PDF
Fill in parameters Public New Hash table (int initial Capacity, floa.pdf
PDF
Given the code below create a method called, getCollisionCount that .pdf
PDF
I have created a class hasdhedDictionary that implements the Diction.pdf
PDF
using the code below create a method called getCollisionCount that w.pdf
PDF
public class LinkedHashEntry { private int key; private int valu.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
using the code below write the public V add(K key, V value); that ad.pdf
Fill in parameters Public New Hash table (int initial Capacity, floa.pdf
Given the code below create a method called, getCollisionCount that .pdf
I have created a class hasdhedDictionary that implements the Diction.pdf
using the code below create a method called getCollisionCount that w.pdf
public class LinkedHashEntry { private int key; private int valu.pdf

Similar to Complete code in Java The hashtable you'll be making will use String.pdf (20)

PDF
This class will implement a hash table. The hash table will hold data.pdf
PPTX
Hash table in java
PPT
Google collections api an introduction
PDF
Describe a data structure to represent sets of elements (each element.pdf
PPTX
Map-Interface-and-HashMap-in-Java-A-Deep-Dive.pptx.pptx
PPTX
How Hashmap works internally in java
PDF
Google Guava for cleaner code
PPTX
Java Foundations: Maps, Lambda and Stream API
PPTX
22.collections(1)
PDF
Do the following program in C++- Create a item class... with and i.pdf
PPTX
LJ_JAVA_FS_Collection.pptx
PPTX
Reference Guide to Java - Day4
PDF
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
PPT
Maps&hash tables
PPTX
Java Collections.pptx
PPTX
collection framework in java
PDF
6. Generics. Collections. Streams
PPTX
javaprograming-COLLECTION FRAMEWORK-171012084019.pptx
PDF
CS-102 Data Structures HashFunction CS102.pdf
PPT
WDD_lec_06.ppt
This class will implement a hash table. The hash table will hold data.pdf
Hash table in java
Google collections api an introduction
Describe a data structure to represent sets of elements (each element.pdf
Map-Interface-and-HashMap-in-Java-A-Deep-Dive.pptx.pptx
How Hashmap works internally in java
Google Guava for cleaner code
Java Foundations: Maps, Lambda and Stream API
22.collections(1)
Do the following program in C++- Create a item class... with and i.pdf
LJ_JAVA_FS_Collection.pptx
Reference Guide to Java - Day4
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
Maps&hash tables
Java Collections.pptx
collection framework in java
6. Generics. Collections. Streams
javaprograming-COLLECTION FRAMEWORK-171012084019.pptx
CS-102 Data Structures HashFunction CS102.pdf
WDD_lec_06.ppt
Ad

More from aarifi9988 (20)

PDF
Do you think that management of an Alaska Native Regional Corporation.pdf
PDF
Draw the punnott square of RrYyx rryy and grve the genatype ratio and.pdf
PDF
Consider an environment in which there is a one-to-one mapping between.pdf
PDF
A population has a mean -161 and a standard deviation -29- Find the me.pdf
PDF
4- X and Y are joint continuous random variables and their joint p-d-f.pdf
PDF
The human body usually responds to mutated cells byA replicating the m.pdf
PDF
The disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
PDF
See picture above What is the cardinality of the relationship between.pdf
PDF
How many different 6 -letter passwords can be formed from the letters.pdf
PDF
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
PDF
ll of the following are true of gonorrhea in women EXCEPT- Primary sit.pdf
PDF
Lahars are hot mudflows caused by the mixing of volcanic ash and rain.pdf
PDF
Match each item with a statement below- Using this network mode within.pdf
PDF
Jump to level 1 What properties apply to each attribute-A programmer m.pdf
PDF
Which of the following methods is the most appropriate when data have.pdf
PDF
Which of these bacteria most likely does not possess the enzymes super.pdf
PDF
There are n-10 battery packs in stock- Each battery has the ability to.pdf
PDF
The pandemic has required organizations to redefine- redesign- and shi.pdf
PDF
The general journals are- Select one- a- Used to record similar repeti.pdf
PDF
The blood vessels in the legs are twice as long as the blood vessels i.pdf
Do you think that management of an Alaska Native Regional Corporation.pdf
Draw the punnott square of RrYyx rryy and grve the genatype ratio and.pdf
Consider an environment in which there is a one-to-one mapping between.pdf
A population has a mean -161 and a standard deviation -29- Find the me.pdf
4- X and Y are joint continuous random variables and their joint p-d-f.pdf
The human body usually responds to mutated cells byA replicating the m.pdf
The disease cystic fibrosis is a recessive trait- Two healthy parents.pdf
See picture above What is the cardinality of the relationship between.pdf
How many different 6 -letter passwords can be formed from the letters.pdf
Problem 3-70 A Part II Comprehensive Problem- Reviewing the Accounting.pdf
ll of the following are true of gonorrhea in women EXCEPT- Primary sit.pdf
Lahars are hot mudflows caused by the mixing of volcanic ash and rain.pdf
Match each item with a statement below- Using this network mode within.pdf
Jump to level 1 What properties apply to each attribute-A programmer m.pdf
Which of the following methods is the most appropriate when data have.pdf
Which of these bacteria most likely does not possess the enzymes super.pdf
There are n-10 battery packs in stock- Each battery has the ability to.pdf
The pandemic has required organizations to redefine- redesign- and shi.pdf
The general journals are- Select one- a- Used to record similar repeti.pdf
The blood vessels in the legs are twice as long as the blood vessels i.pdf
Ad

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
master seminar digital applications in india
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 4: Burden of Disease Tutorial Slides S2 2025
STATICS OF THE RIGID BODIES Hibbelers.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Anesthesia in Laparoscopic Surgery in India
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
01-Introduction-to-Information-Management.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
master seminar digital applications in india
Final Presentation General Medicine 03-08-2024.pptx
Institutional Correction lecture only . . .

Complete code in Java The hashtable you'll be making will use String.pdf

  • 1. Complete code in Java The hashtable you'll be making will use Strings as the keys and Object as the values. Similar to linked lists, by storing Object as values, you can store any kind of object in the hashtable. package Dictionary; import java.util.ArrayList; import java.util.Hashtable; import List.ListInterface; import List.MyLinkedList; public class MyHashtable implements DictionaryInterface { protected int tableSize; protected int size; // The LinkedList is of type Entry protected MyLinkedList[] table; public MyHashtable(int tableSize) { table = new MyLinkedList[tableSize]; this.tableSize = tableSize; } public int biggestBucket() { int biggestBucket = 0; for(int i = 0; i < table.length; i++) { // Loop through the table looking for non-null locations. if (table[i] != null) { // If you find a non-null location, compare the bucket size against the largest
  • 2. // bucket size found so far. If the current bucket size is bigger, set biggestBucket // to this new size. MyLinkedList bucket = table[i]; if (biggestBucket < bucket.size()) biggestBucket = bucket.size(); } } return biggestBucket; // Return the size of the biggest bucket found. } // Returns the average bucket length. Gives a sense of how many collisions are happening overall. public float averageBucket() { float bucketCount = 0; // Number of buckets (non-null table locations) float bucketSizeSum = 0; // Sum of the size of all buckets for(int i = 0; i < table.length; i++) { // Loop through the table if (table[i] != null) { // For a non-null location, increment the bucketCount and add to the bucketSizeSum MyLinkedList bucket = table[i]; bucketSizeSum += bucket.size(); bucketCount++; } } // Divide bucketSizeSum by the number of buckets to get an average bucket length.
  • 3. return bucketSizeSum/bucketCount; } public String toString() { String s = ""; for(int tableIndex = 0; tableIndex < tableSize; tableIndex++) { if (table[tableIndex] != null) { MyLinkedList bucket = table[tableIndex]; for(int listIndex = 0; listIndex < bucket.size(); listIndex++) { Entry e = (Entry)bucket.get(listIndex); s = s + "key: " + e.key + ", value: " + e.value + "n"; } } } return s; } protected class Entry { String key; Object value; Entry(String key, Object value) { this.key = key; this.value = value;
  • 4. } } // Implement these methods public boolean isEmpty() {return true;} // returns true if the Dictionary is empty, false otherwise. public int size(){return -1;} //Returns the number of key/value pairs stored in the dictionary. // Adds a value stored under the given key. If the key has already been stored in the Dictionary, // replaces the value associated with the key and returns the old value. If the key isn't in the dictionary // returns null. public Object put(String key, Object value){ // 1. Compute an array index given the key // 2. If that location in the table is null, // that means nothing has been previously stored using a key with this hash code. // a. Create a new MyLinkedList to be the bucket. // b. Add the new Entry for the key/value pair to the list. // c. Set this location in the array equal to the new bucket (list). // d. Increment the size (the number of unique keys you have stored). // 3. If the location in the table isn't null, // that means keys with this colliding hash code have been previously stored. // 3a, a value exists for the key // a. Linearly search through the bucket (the list) stored at this array // location comparing the key for each entry with the key passed into put(). // If you get a match, this means this key as been previously stored. // Save the old value in the Entry (so you can return it) and replace it with the new value.
  • 5. // (use the code below) // Entry oldValue = new Entry(key, e.value); // e.value = value; // NOTE: this is technically not correct as you would need to create a deep copy of the entry, // however, that is outside the realm of this assignment. The code above will be // enough to complete the assignment // return old value here // return oldValue.value; // 3b, a value does not exist for the key // b. If you don't find the key in the bucket, // then just add a new Entry (with the key and value) to the beginning of the list. // Increment the size. return null; } public Object get(String key){ // 1. Compute an array index given the key // 2. If that location in the table is null, // that means nothing has been stored using a key with this hash code. // So we can return null. // 4. Linearly search through the bucket (the list), // comparing the key for each entry with the key passed into get(). // Extracting each element in // the Linked List
  • 6. // If you find a match, return the value. return null; } // Retrieves the value stored with the key. public void remove(String key){ // 1. Compute an array index given the key // 2. If that location in the table is null, then this key has definitely not been used to store a value. // 3. If the location in the table has a bucket, // we need to linearly search it to see if it contains an Entry with the key. // If you find an Entry in the bucket (linked list) with the key: // a. Remove this Entry from the bucket. // b. Decrement size (the number of unique keys stored in the hashtable). } // Deletes the key/value pair stored with the given key. public void clear(){} // Empties the dictionary. public String[] getKeys(){ // 1. Create a String[] with a size equal to the number of unique keys in the hashtable // 2. Iterate through the hashtable array. // For each table location that isn't null // a. Iterate though the bucket (linked list) // getting the key out of each Entry and storing it in // the array of strings you created in step 1. // 3. Return the String[] return null; }
  • 7. }