Question
I created a hashtable with hash chaining( but not sure if doing the correct thing ), For each number in numbers.tx t(you can create your
I created a hashtable with hash chaining(but not sure if doing the correct thing ), For each number in numbers.txt(you can create your own text file with numbers for testing) put it into the hashtable. I timed how long this takes (CPU time). My question is how do I determine how many collisions occurred, and also whenver I print the results the output file is empty. I want to Output the build time and the collision report to a text file.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class LinkedHashEntry {
private int key;
private int value;
private PrintWriter writer;
private LinkedHashEntry next;
LinkedHashEntry(int key) {
this.key = key;
writer = null;
this.next = null;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getKey() {
return key;
}
public LinkedHashEntry getNext() {
return next;
}
public void setNext(LinkedHashEntry next) {
this.next = next;
}
public static void main(String[] args)throws IOException {
int key = 0;
LinkedHashEntry myMap = new LinkedHashEntry(key);
myMap.readFile();
}
public class HashMap {
private final int TABLESIZEONE = 11_000;
private final int TABLESIZETWO = 15_707;
private final int TABLESIZETHREEN = 17_111;
private final int TABLESIZEFOUR = 25_111;
private int key;
private int data;
private LinkedHashEntry[] tableone;
private LinkedHashEntry[] tabletwo;
private LinkedHashEntry[] tablethree;
private LinkedHashEntry[] tablefour;
HashMap() {
tableone = new LinkedHashEntry[TABLESIZEONE];
tabletwo = new LinkedHashEntry[TABLESIZETWO];
tablethree = new LinkedHashEntry[TABLESIZETHREEN];
tablefour = new LinkedHashEntry[TABLESIZEFOUR];
int numOne = 0;
int numTwo = 0;
int numThree = 0;
int numFour = 0;
for (int i = 0; i < TABLESIZEFOUR; i++)
tableone[numOne] = null;
tableone[numTwo] = null;
tableone[numThree] = null;
tableone[numFour] = null;
}
public void get(int key) {
int hash = (key % 1231);
if (tableone[hash] != null) {
LinkedHashEntry entry = tableone[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return;
else
entry.getValue();
}
if (tabletwo[hash] != null){
LinkedHashEntry entry = tabletwo[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return;
else
entry.getValue();
}
if (tablethree[hash] != null){
LinkedHashEntry entry = tablethree[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return;
else
entry.getValue();
}
if (tablefour[hash] != null){
LinkedHashEntry entry = tablefour[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return;
else
entry.getValue();
}
}
public void put( int key) throws IOException {
int hash = (key % 1231);
if (tableone[hash] != null) {
LinkedHashEntry entry = tableone[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
if (entry.getKey() == key)
entry.setValue(value);
else
entry.setNext(new LinkedHashEntry(key));
System.out.println("key " + entry.getKey() + " Collision " + hash);
writer.println("key " + entry.getKey() + " Collision " + hash);
}
if (tabletwo[hash] != null) {
LinkedHashEntry entry = tabletwo[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
if (entry.getKey() == key)
entry.setValue(value);
else
entry.setNext(new LinkedHashEntry(key));
System.out.println("key " + entry.getKey() + " Collision " + hash);
writer.println("key " + entry.getKey() + " Collision " + hash);
}
if (tablethree[hash] != null) {
LinkedHashEntry entry = tablethree[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
if (entry.getKey() == key)
entry.setValue(value);
else
entry.setNext(new LinkedHashEntry(key));
System.out.println("key " + entry.getKey() + " Collision " + hash);
writer.println("key " + entry.getKey() + " Collision " + hash);
}
if (tablefour[hash] != null) {
LinkedHashEntry entry = tablefour[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
if (entry.getKey() == key)
entry.setValue(value);
else
entry.setNext(new LinkedHashEntry(key));
System.out.println("key " + entry.getKey() + " Collision " + hash);
writer.println("key " + entry.getKey() + " Collision " + hash);
}
}
}
public void readFile() throws IOException {
HashMap myMap = new HashMap();
String fileName = "numbers.txt";
writer = new PrintWriter("LinkedHashEntryRport.txt");
int integerContent = 0;
File file = new File(fileName);
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()) {
integerContent = sc.nextInt();
myMap.put(integerContent);
}
sc.close();
writer.close();
} catch (FileNotFoundException fnf) {
System.out.println("File not found.");
System.exit(0);
}
catch (Exception e) {
System.out.println(" Program terminated Safely...");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started