0% found this document useful (0 votes)
62 views

Hashtable Module 5

This document describes a Java program that implements a hash table using direct addressing and separate chaining for collision resolution. It defines a HashTable class with methods to add key-value pairs to the table, retrieve a value by key, and resize the table if it becomes too full. The main method tests the put and get methods by prompting the user to add and retrieve entries from the hash table.

Uploaded by

Ram Guggul
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Hashtable Module 5

This document describes a Java program that implements a hash table using direct addressing and separate chaining for collision resolution. It defines a HashTable class with methods to add key-value pairs to the table, retrieve a value by key, and resize the table if it becomes too full. The main method tests the put and get methods by prompting the user to add and retrieve entries from the hash table.

Uploaded by

Ram Guggul
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

import java.io.

*;
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)
{

Hashtable ht = new Hashtable();

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 );
}

/* Method to clear EVM */


public void clear()
{
int l = arr.length;
arr = new int[l];
}
/* Method to print DAT */
public void printTable()
{
System.out.println("\n Display Direct Addressing Table : ");
int l = arr.length;
for (int i = 0; i < l; i++)
if (arr[i] != 0)
System.out.println(i +" "+ arr[i]);
System.out.println();
}
}

public class ElectionCount


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("EVM using Direct Addressing Table \n\n");

/* 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");

int choice = scan.nextInt();


switch (choice)
{
case 1 :
System.out.println("Enter Voter_id and Condidate_id");
dat.ADD( scan.nextInt(), scan.nextInt() );
break;
case 2 :
System.out.println("Enter Voter_id: " );
dat.FIND( scan.nextInt() );
break;
case 3 :
System.out.println("Enter Voter_id: " );
dat.COUNT( scan.nextInt() );
break;
case 4 :
dat.clear();
System.out.println("EVM Cleared\n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* To Display hash table */
dat.printTable();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
--------------------------------------------------------------------------------

public static void main(String[] args) {


Hashtable<String, Integer> names = new Hashtable<String, Integer>();
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String name = in.next();
int age = in.nextInt();
names.put(name, age);
}
}
-----------------------------------------------------------------------------------
----------
"c:/users/toshiba/desktop/MyText.txt"
"C:\\Users\\pankaj\\Desktop\\test.txt"

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.*;

public class HashTable


{

public static void main(String[] args)


{
BufferedReader in = new BufferedReader( new FileReader("C:\\Users\\G D
Ram Reddy\\Desktop\\data.txt"));
Hashtable hash = new Hashtable();

//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);

}
}

String[] arr=br.readLine().split(" ");


int[] intarr=new int[arr.length];
for(int i=0;i<arr.length;i++)
intarr[i]=Integer.parseInt(arr[i]);

---------------------------------------------------------------------------------
public class HashTable
{

static private class ListNode


{

Object key;
Object value;
ListNode next; // Pointer to next node in the list;
// A null marks the end of the list.
}

private ListNode[] table;


private int count; // The number of (key,value) pairs in the hash table

public HashTable()
{
// Create a hash table with an initial size of 64.
table = new ListNode[64];
}

public HashTable(int initialSize)


{
// Create a hash table with a specified initial size.
// Precondition: initalSize > 0.
table = new ListNode[initialSize];
}

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()

public void put(Object key, Object value)


{

int bucket = hash(key); // Which location should this key be in?


ListNode list = table[bucket]; // For traversing the linked list
// at the appropriate location.
while (list != null)
{
// Search the nodes in the list, to see if the key already exists.
if (list.key.equals(key))
break;
list = list.next;
}
// At this point, either list is null, or list.key.equals(key).
if (list != null) {
// Since list is not null, we have found the key.
// Just change the associated value.
list.value = value;
}
else {
// Since list == null, the key is not already in the list.
// Add a new node at the head of the list to contain the
// new key and its associated value.
if (count >= 0.75*table.length) {
// The table is becoming too full. Increase its size
// before adding the new node.
resize();
}
ListNode newNode = new ListNode();
newNode.key = key;
newNode.value = value;
newNode.next = table[bucket];
table[bucket] = newNode;
count++; // Count the newly added key.
}
}

public Object get(Object key) {


// Retrieve the value associated with the specified key
// in the table, if there is any. If not, the value
// null will be returned.
int bucket = hash(key); // At what location should the key be?
ListNode list = table[bucket]; // For traversing the list.
while (list != null) {
// Check if the specified key is in the node that
// list points to. If so, return the associated value.
if (list.key.equals(key))
return list.value;
list = list.next; // Move on to next node in the list.
}
// If we get to this point, then we have looked at every
// node in the list without finding the key. Return
// the value null to indicate that the key is not in the table.
return null;
}
}

public class TestHashTable


{

public static void main(String[] args)


{
HashTable table = new HashTable(2);
String key,value;
while (true)
{
System.out.println("\nMenu:");
System.out.println(" 1. test put(key,value)");
System.out.println(" 2. test get(key)");
//System.out.println(" 3. test containsKey(key)");
//System.out.println(" 4. test remove(key)");
System.out.println(" 5. show complete contents of hash table.");
//System.out.println(" 6. EXIT");
System.out.print("Enter your command: ");
switch ( TextIO.getlnInt())

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.*;

public class EVMHashTable


{

public static void main(String[] args)


{
BufferedReader in = new BufferedReader( new
FileReader("D:\\Users\\RGUGGULLA2\\Desktop\\data.txt"));
Hashtable hash = new Hashtable();

//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;

public class HelloWorld


{

public static void main(String[] args) throws IOException


{
Hashtable<String, String> map = new Hashtable<String, String>();
BufferedReader in = new BufferedReader(new FileReader("data.txt"));
String line = "";
while ((line = in.readLine()) != null) {
String parts[] = line.split("\t");
map.put(parts[0], parts[1]);
}
in.close();
System.out.println(map.toString());
}

151020 130
151021 135
151022 132
151023 135
151024 130
151025 135
151026 130
151027 135
151028 132
151029 130
151030 135
151031 135

You might also like