Implement the java code with data structure supporting listed functionalities using object-oriented principles
.Hash Table
//---------- Functions.java ---------- abstract class
public abstract class Functions {
public abstract void Insert(int key, String name);
public abstract void Delete(int key);
}
//---------- Main.java ----------
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class People extends Functions{
Map<Integer, String> person = new HashMap<Integer, String>();
@Override
public void Insert(int key, String name) {
person.put(key, name);
}
@Override
public void Delete(int key) {
person.remove(key);
}
private void Contains(){
System.out.println(person.values());
}
private void GetValueByKey(int key){
System.out.println(person.get(key));
}
private void Size(){
System.out.println(person.size());
}
public void ShowAll(Scanner in){
Contains();
System.out.print("Enter search key: ");
GetValueByKey(in.nextInt());
Size();
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
People people = new People();
System.out.print("Enter the number of elements: ");
int count = in.nextInt();
for(int i = 0; i < count; i++){
System.out.print("Enter " + (i+1) + " key: ");
int key = in.nextInt();
System.out.print("Enter " + (i+1) + " name: ");
people.Insert(key,in.next());
}
System.out.print("Enter the key to delete: ");
int keyDel = in.nextInt();
people.Delete(keyDel);
people.ShowAll(in);
}
}
Comments
Leave a comment