Hashing Examples
The example demonstrate how to create your own Hash tables
This is the map interface
/* * This is the map interface */ /** * * @author JJ */ public interface Map { //Remove all objects from the map void clear(); /* * Returns true if the map contains a mapping for the * Specific key i.e. contain an object whose key is the given String key */ boolean containsKey(String key); //If the map is empty return true boolean empty(); //Return the size of the map int size(); //Returns the object in the table whose key is the given string Object get(String key); //Insert the object into the map void insert(Object ele, Object v); }
This is the HashTable
/* * This is the HashTable that implements the map */ /** * * @author JJ */ import java.util.*; public class HashTable { LinkedList[] separateChain; int numberObjects=0; int bucketSize; //Default constructor of the HashTable public HashTable(int size) { bucketSize = size; separateChain = new LinkedList[size]; for(int j=0; j
This is the student driver
/* * This is the student class */ /** * * @author JJ */ public class Student { public static String ssn,dob; static String firstName, lastName, address; public Student(String getSSN, String getDOB, String getFirst, String getLast, String getAddress) { ssn = getSSN; dob = getDOB; firstName = getFirst; lastName = getLast; address = getAddress; } @Override public int hashCode() { return ssn.hashCode(); } @Override public boolean equals(Object o) { if(this==o) return true; else return false; } public String toString() { String str; str = "SSN: " + ssn + " DOB: " + dob + " Name: " + firstName + " " + lastName + "\nAddress: " + address; return str; } }
Now lets create a driver class to test the HashTable
/* */ /** * * @author JJ */ import java.util.*; public class HashTableDriver { public static void main(String[] args) { Scanner key = new Scanner(System.in); HashTable ht = new HashTable(10); int selection; do { try { System.out.println("1) Create ID 2) Getinfo using SSN 3) Clear 4) Display 5) Exit"); System.out.print("Enter your seleciton: "); selection = key.nextInt(); if(selection==1) { System.out.println("\n"); System.out.print("Enter your social: "); String ssN = key.next(); System.out.print("Enter date of birth: "); String bday = key.next(); System.out.print("Enter first name: "); String fName = key.next(); System.out.print("Enter last name: "); String lName = key.next(); System.out.print("Enter address: "); String address = key.next(); Student s1 = new Student(ssN, bday, fName, lName, address); ht.insert(s1); } if(selection==2) { System.out.println("Enter the SSN: "); String getssN = key.next(); System.out.println(ht.get(getssN)); } if(selection==3) { ht.clear(); System.out.println("The HashTable is empty now"); } if(selection==4) { ht.print(); } } catch(InputMismatchException e) { System.out.println("Invalid Input"); System.out.print("Enter your seleciton again: "); selection = key.nextInt(); } }while(selection != 5); } }
< Java > <Home>< Java Programs >