用java語言寫hashtable 理解底層的原理
目前是 數組 + 鏈表組成的原理
如果代碼有問題,希望在評論中指出,互相學習
主要的功能
添加 員工 刪除 員工 顯示 員工 查找 員工
```java
import java.util.HashMap;
import java.util.Scanner;
public class HashTableDemo {
public static void main(String[] args) {
int no;
String name;
Emp emp = null;
HashTable hashTable = new HashTable(7);
char key = ' ';
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("(a)add添加成員");
System.out.println("(s)show顯示成員");
System.out.println("(e)exit退出程序");
System.out.println("(f)find查找成員");
System.out.println("(d)del刪除成員");
key = scanner.next().charAt(0);
switch (key){
case 'a':
System.out.println("請輸入學號");
no= scanner.nextInt();
System.out.println("請輸入你的名字");
name = scanner.next();
emp = new Emp(no,name);
hashTable.addEmp(emp);
break;
case 's':
hashTable.showList();
break;
case 'f':
System.out.println("請輸入學號");
int number = scanner.nextInt();
hashTable.findEmpByNo(number);
break;
case 'd':
System.out.println("請輸入學號");
int nb = scanner.nextInt();
hashTable.delEmpByNo(nb);
break;
case 'e':
scanner.close();
System.exit(0);
default:
break;
}
}
}
}
class HashTable{
private LinkListEmp[] linkListEmps;
private int size;
public HashTable(int size){
linkListEmps = new LinkListEmp[size];
for (int i = 0; i<size;i++){
linkListEmps[i] = new LinkListEmp();
}
this.size = size;
}
public void addEmp(Emp newEmp){
int i = hashFun(newEmp.no);
linkListEmps[i].add(newEmp);
}
public int hashFun(int no){
return no%size;
}
public void showList(){
for (int i=0;i<size;i++){
linkListEmps[i].showList(i);
}
}
public void findEmpByNo(int no){
int i = hashFun(no);
Emp empByno = linkListEmps[i].findEmpByno(no);
if(empByno != null) {//找到
System.out.printf("在第%d條鏈表中找到 僱員 id = %d\n", (i + 1), no);
}else{
System.out.println("在哈希表中,沒有找到該僱員~");
}
}
public void delEmpByNo(int no){
int i = hashFun(no);
linkListEmps[i].delEmp(no);
}
}
class LinkListEmp{
Emp head = null;
public void add(Emp newEmp){
if (head == null){
head = newEmp;
return;
}
Emp curEmp = head;
while (true){
if (curEmp.next == null){
break;
}
curEmp = curEmp.next;
}
curEmp.next = newEmp;
}
//展示鏈表的
public void showList(int i){
if(head == null){
System.out.println("第 "+(i+1)+" 鏈表爲空");
return;
}
System.out.print("第 "+(i+1)+" 鏈表的信息爲");
Emp curEmp = head;
while (true){
System.out.printf(" => id=%d name=%s\t",curEmp.no,curEmp.name);
if (curEmp.next == null){
break;
}
curEmp = curEmp.next;
}
System.out.println();
}
public Emp findEmpByno(int no){
if (head == null){
System.out.println("鏈表爲空,找不到你要的元素");
return null;
}
Emp curEmp = head;
while (true){
if (curEmp.no == no){
System.out.printf("%d號的元素被找到,他的名字叫%s",curEmp.no,curEmp.name);
break;
}
if (curEmp.next == null){
System.out.printf("%d號的元素無法被找到",curEmp.no);
break;
}
curEmp = curEmp.next;
}
return curEmp;
}
public void delEmp(int no){
if (head == null){
System.out.println("無此員工");
return;
}
if (head.no == no){
head = null;
return;
}
Emp curEmp = head;
while (true){
if (curEmp.next == null){
System.out.println("未找到元素,無法刪除");
}
if (curEmp.next.no == no){
curEmp.next = curEmp.next.next;
break;
}
curEmp = curEmp.next;
}
}
}
class Emp{
public int no;
public String name;
public Emp next;
public Emp(int no,String name){
this.name = name;
this.no = no;
}
@Override
public String toString() {
return "Emp{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}