基本介绍
散列表(Hash Table,也叫哈希表),是根据关键码值(Key Value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中的一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表

Google上机题
有一个公司,当有新员工来报到时,要求将该员工的信息加入(id,性别,年龄,地址….),当输入该员工的id时,要求查找该员工的所有信息。
要求:不使用数据库,尽量节省内存,速度越快越好。
package com.xie.hashtable;
import java.util.Scanner;
public class HashTableDemo {
public static void main(String[] args) {
//创建一个HashTab
HashTab hashTab = new HashTab(7);
String key = "";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("add:添加雇员");
System.out.println("list:显示雇员");
System.out.println("find:查找雇员");
System.out.println("delete:删除雇员");
System.out.println("exit:退出程序");
key = scanner.next();
switch (key) {
case "add":
System.out.println("输入id");
int id = scanner.nextInt();
System.out.println("输入name");
String name = scanner.next();
Emp emp = new Emp(id, name);
hashTab.add(emp);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.println("请输入编号");
int no = scanner.nextInt();
hashTab.findEmpById(no);
break;
case "delete":
System.out.println("请输入编号");
int deleteNo = scanner.nextInt();
hashTab.deleteEmpById(deleteNo);
break;
case "exit":
scanner.close();
System.exit(0);
break;
default:
break;
}
}
}
}
//创建哈希表,管理多条链表
class HashTab {
private int size;
private EmpLinkedList[] empLinkedListArray;
public HashTab(int size) {
this.size = size;
empLinkedListArray = new EmpLinkedList[size];
for (int i = 0; i < size; i++) {
empLinkedListArray[i] = new EmpLinkedList();
}
}
//添加雇员
public void add(Emp emp) {
//根据员工的id,得到该员工应当添加到哪条链表
int empLinkedListNo = hashFun(emp.id);
//将emp添加到对应的链表
empLinkedListArray[empLinkedListNo].add(emp);
}
//查找员工
public Emp findEmpById(int id) {
int no = hashFun(id);
Emp empById = empLinkedListArray[no].findEmpById(id);
if (empById == null) {
System.out.println("不存在id=" + id + "元素");
return null;
} else {
System.out.println("存在id=" + id + ",name=" + empById.name);
return empById;
}
}
//删除雇员
public void deleteEmpById(int id) {
int no = hashFun(id);
empLinkedListArray[no].deleteEmp(id);
}
//遍历哈希表
public void list() {
for (int i = 0; i < size; i++) {
empLinkedListArray[i].list(i);
}
}
//编写散列函数,使用简单的取模法
private int hashFun(int id) {
return id % size;
}
}
//表示雇员
class Emp {
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
//表示链表
class EmpLinkedList {
//头指针
private Emp head;
//添加雇员到链表
//说明:
//1.当添加雇员时,id时自增的,即id分配总是从小到大,因此我们将该雇员直接加到本链表的最后即可
public void add(Emp emp) {
//如果是添加第一个雇员
if (head == null) {
head = emp;
} else {
Emp curr = head;
while (true) {
if (curr.next == null) {
break;
}
curr = curr.next;
}
curr.next = emp;
}
}
//遍历
public void list(int no) {
if (head == null) {
System.out.println("第" + (no + 1) + "链表为空");
return;
}
System.out.print("第" + (no + 1) + "链表信息为:");
Emp curr = head;
while (true) {
if (curr != null) {
System.out.printf("=> id=%d,name=%s\t", curr.id, curr.name);
curr = curr.next;
} else {
break;
}
}
System.out.println();
}
//根据id查找雇员
public Emp findEmpById(int id) {
if (head == null) {
System.out.println("链表为空");
return null;
}
Emp temp = head;
while (temp != null) {
if (temp.id == id) {
return temp;
}
temp = temp.next;
}
return null;
}
//根据id删除雇员
public void deleteEmp(int id) {
if (head == null) {
System.out.println("没有id=" + id + "的雇员");
return;
}
Emp curr = head;
boolean flag = false;
while (true) {
if (curr == null) {
break;
}
if (curr.next.id == id) {
flag = true;
break;
}
curr = curr.next;
}
if (flag) {
curr.next = curr.next.next;
} else {
System.out.println("没有id=" + id + "的雇员");
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
【编辑推荐】