Hashtable Module 5
Hashtable Module 5
*;
import java.util.*;
class DirectAddressingTab
{
private int[] arr ;
private final int DEFAULT_CAPACITY = 151032;
/* Constructor */
public DirectAddressingTab()
{
arr = new int[DEFAULT_CAPACITY];
}
/* Constructor */
public DirectAddressingTab(int capacity)
{
arr = new int[capacity + 1];
}
/* ADD Method to add voter_id and condidate id to the table*/
public void ADD(int voter_id, int candidate_id)
{
ht.put(voter_id, candidate_id);
System.out.println("entries in table: " + ht);
Set sKey = ht.keySet();
System.out.println("key set: " + sKey);
System.out.println("Voter id and condidate id are added to the table
sucessfully: "+ voter_id+" "+candidate_id );
}
/* FIND Method takes a voter_id as input and outputs the condidate_id for whom
the vote was cast */
public void FIND(int voter_id)
{
System.out.println("Entered voter_id is: "+ voter_id);
System.out.println("your vote has been casted to condidate_id: "+
arr[voter_id] );
}
/* COUNT Method takes a condidate_id as input and output the total number of
votes received by him/her */
public void COUNT(int condidate_id)
{
int l = arr.length;
int count = 0;
for (int i = 0; i < l; i++)
if (arr[i] == condidate_id )
count++;
System.out.println("Number of Votes Received by condidate_id:"+
condidate_id +" "+"is: "+ count );
}
/* object of DirectAddressingTab */
DirectAddressingTab dat = new DirectAddressingTab();
char ch;
/* Perform DirectAddressingTable operations */
do
{
System.out.println("\nElectronic Voting Machine Operations\n");
System.out.println("1. ADD ");
System.out.println("2. FIND ");
System.out.println("3. COUNT ");
System.out.println("4. clear");
Hashtable< Integer, String > hash = new Hashtable< Integer, String >();
BufferedReader rd = new BufferedReader( new FileReader ("students.txt"));
String line;
int i = 0;
while ((line = rd.readLine()) != null){
hash.put(i, line);
i++;
}
for ( int j = 0 ; j < hash.size() ; j++){
System.out.println(hash.get(j));
}
----------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.util.*;
//String line;
int[] values;
//String[] arr=br.readLine().split(" ");
while(((line = in.readLine()) != null))
{
//line = in.readLine();
//values = line.split(" ");
String[] arr=in.readLine().split(" ");
int[] intarr=new int[arr.length];
for(int i=0;i<arr.length;i++)
intarr[i]=Integer.parseInt(arr[i]);
int voter_id = intarr[0];
int candidate_id = intarr[1];
hash.put(voter_id, candidate_id);
}
// checking hash table h
System.out.println("after clearing: " + hash);
}
}
---------------------------------------------------------------------------------
public class HashTable
{
Object key;
Object value;
ListNode next; // Pointer to next node in the list;
// A null marks the end of the list.
}
public HashTable()
{
// Create a hash table with an initial size of 64.
table = new ListNode[64];
}
void dump()
{
System.out.println();
for (int i = 0; i < table.length; i++) {
// Print out the location number and the list of
// key/value pairs in this location.
System.out.print(i + ":");
ListNode list = table[i]; // For traversing linked list number i.
while (list != null)
{
System.out.print(" (" + list.key + "," + list.value + ")");
list = list.next;
}
System.out.println();
}
} // end dump()
case 1:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.print(" Value = ");
value = TextIO.getln();
table.put(key,value);
break;
case 2:
System.out.print("\n Key = ");
key = TextIO.getln();
System.out.println(" Value is " + table.get(key));
break;
//case 3:
//System.out.print("\n Key = ");
//key = TextIO.getln();
//System.out.println(" containsKey(" + key + ") is "
+ table.containsKey(key));
//break;
//case 4:
//System.out.print("\n Key = ");
//key = TextIO.getln();
//table.remove(key);
//break;
case 5:
table.dump();
break;
case 6:
return; // End program by returning from main()
default:
System.out.println(" Illegal command.");
break;
}
System.out.println("\nHash table size is " + table.size());
}
}
-----------------------------------------------------------------------------------
-----------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.util.*;
//String line;
int[] values;
//String[] arr=br.readLine().split(" ");
while(((in.readLine()) != null))
{
//line = in.readLine();
//values = line.split(" ");
String[] arr=line.split("\t");
int[] intarr=new int[arr.length];
for(int i=0;i<arr.length;i++)
intarr[i]=Integer.parseInt(arr[i]);
int voter_id = intarr[0];
int candidate_id = intarr[1];
hash.put(voter_id, candidate_id);
}
// checking hash table h
System.out.println("after clearing: " + hash);
}
}
-----------------------------------------------------------------------------------
-------------------------------------------
package com.sourcecode.example;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Collectors;
151020 130
151021 135
151022 132
151023 135
151024 130
151025 135
151026 130
151027 135
151028 132
151029 130
151030 135
151031 135