0% found this document useful (0 votes)
2 views9 pages

Hash Table

The document presents implementations of hash tables using two methods: separate chaining and linear probing, along with examples of their usage. It also includes a program to consolidate student grades from a dataset and another to represent a graph using an adjacency list with a hash table. Each section demonstrates the functionality of the respective data structures and their applications in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Hash Table

The document presents implementations of hash tables using two methods: separate chaining and linear probing, along with examples of their usage. It also includes a program to consolidate student grades from a dataset and another to represent a graph using an adjacency list with a hash table. Each section demonstrates the functionality of the respective data structures and their applications in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Separate Chaining:

import [Link];

public class HashTable {

class Node {

int key, value;

Node(int key, int value) {

[Link] = key;

[Link] = value;

LinkedList<Node>[] table;

int size;

HashTable(int size) {

[Link] = size;

table = new LinkedList[size];

for (int i = 0; i < size; i++) {

table[i] = new LinkedList<>();

int hash(int key) {

return key % size;

void put(int key, int value) {

int index = hash(key);

table[index].add(new Node(key, value));

void get(int key) {


int index = hash(key);

for (Node n : table[index]) {

if ([Link] == key) {

[Link]("Value = " + [Link]);

return;

[Link]("Key not found");

void remove(int key) {

int index = hash(key);

for (Node n : table[index]) {

if ([Link] == key) {

table[index].remove(n);

break;

void display() {

for (int i = 0; i < size; i++) {

[Link](i + " : ");

for (Node n : table[i]) {

[Link]("(" + [Link] + "," + [Link] + ") ");

[Link]();

}
}

public static void main(String[] args) {

HashTable ht = new HashTable(5);

[Link](1, 10);

[Link](6, 20);

[Link](11, 30);

[Link](2, 40);

[Link]("Hash Table:");

[Link]();

[Link]("\nSearch:");

[Link](6);

[Link]("\nAfter Deleting Key 6:");

[Link](6);

[Link]();

}
Linear Probing:

class HashTable {

static class Entry {

int key;

int value;

Entry(int key, int value) {

[Link] = key;

[Link] = value;

private Entry[] table;

private int size;

public HashTable(int size) {

[Link] = size;

table = new Entry[size];

private int hash(int key) {

return key % size;

public void put(int key, int value) {

int index = hash(key);

while (table[index] != null && table[index].key != key) {

index = (index + 1) % size;

table[index] = new Entry(key, value);

}
public Integer get(int key) {

int index = hash(key);

int start = index;

while (table[index] != null) {

if (table[index].key == key)

return table[index].value;

index = (index + 1) % size;

if (index == start)

break;

return null;

public void remove(int key) {

int index = hash(key);

int start = index;

while (table[index] != null) {

if (table[index].key == key) {

table[index] = null;

return;

index = (index + 1) % size;

if (index == start)

break;

public void display() {

for (int i = 0; i < size; i++) {


if (table[i] != null)

[Link](i + " -> (" + table[i].key + "," + table[i].value + ")");

else

[Link](i + " -> null");

public static void main(String[] args) {

HashTable ht = new HashTable(7);

[Link](10, 100);

[Link](17, 200);

[Link](24, 300);

[Link](31, 400);

[Link]();

[Link]([Link](24));

[Link](24);

[Link]();

}
You have a stream of data in this format <Student_name, Course, Grade>. Consolidate this
data to print the Grade Sheet for each student.

import [Link].*;

class HashTable {

public static void main(String[] args) {

String[][] data = {

{"Sri", "Java", "A"},

{"Lali", "Python", "A+"},

{"Sow", "DBMS", "B+"},

{"Sri", "C++", "A"},

{"Siva", "Java", "B"},

{"Lali", "DBMS", "A"},

{"Sow", "Java", "A"},

{"Siva", "Python", "B+"}

};

HashMap<String, ArrayList<String>> map = new HashMap<>();

for (String[] record : data) {

String student = record[0];

String details = record[1] + " : " + record[2];

[Link](student, new ArrayList<>());

[Link](student).add(details);

for ([Link]<String, ArrayList<String>> entry : [Link]()) {

[Link]("Student: " + [Link]());

for (String s : [Link]()) {

[Link](s);
}

[Link]();

Implement the Adjacency List representation of a graph using a HashTable.

import [Link].*;

public class HashTable{

public static void main(String[] args){

HashMap<String,ArrayList<String>> graph=new HashMap<>();

[Link]("Sri",new ArrayList<>());

[Link]("Lali",new ArrayList<>());

[Link]("Sow",new ArrayList<>());

[Link]("Siva",new ArrayList<>());

[Link]("Sri").add("Lali");

[Link]("Sri").add("Sow");

[Link]("Lali").add("Sri");
[Link]("Lali").add("Siva");

[Link]("Sow").add("Sri");

[Link]("Sow").add("Siva");

[Link]("Siva").add("Lali");

[Link]("Siva").add("Sow");

for(String vertex:[Link]()){

[Link](vertex+" -> "+[Link](vertex));

You might also like