散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。 2、实际需求
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,姓名,住址.),当输入该员工的id时,要求查找到该员工的所有信息.
要求:
不使用数据库,尽量节省内存,速度越快越好 => 哈希表(散列)添加时,保证按照id从低到高插入
实现:
使用【 数组+链表】 来实现哈希表, 数组中的元素存放对应的链表链表不带表头 [即:链表的第一个结点就存放雇员信息] 3、代码实现
import java.util.Scanner;
public class HashTableDemo {
public static void main(String[] args) {
//创建哈希表
HashTable hashTable = new HashTable(7);
//写一个简单菜单
String key = "";
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("add:添加雇员");
System.out.println("delete:删除雇员");
System.out.println("list:显示雇员");
System.out.println("find:查找系统");
System.out.println("exit:退出系统");
key = scanner.next();
switch(key){
case "add":
System.out.println("输入id");
int id = scanner.nextInt();
System.out.println("请输入名字");
String name = scanner.next();
//创建雇员
Employee employee = new Employee(id, name);
hashTable.add(employee);
break;
case "delete":
System.out.println("请输入id");
id = scanner.nextInt();
hashTable.deleteEmployee(id);
break;
case "list":
hashTable.list();
break;
case "exit":
scanner.close();
System.exit(0);
case "find":
System.out.println("请输入id");
id = scanner.nextInt();
hashTable.findEmployeeById(id);
break;
default:
break;
}
}
}
}
//创建HashTable 管理多条链表
class HashTable {
private EmployeelinkedList[] employeelinkedListArray;
private int size; //表示数组的长度,即有多少条链表
//构造器
public HashTable(int size){
this.size = size;
//初始化employeelinkedListArray
employeelinkedListArray = new EmployeelinkedList[size];
//这时要初始化每个链表,将每条链表添加到数组里
for (int i = 0; i < size; i++) {
employeelinkedListArray[i] = new EmployeelinkedList();
}
}
//添加雇员
public void add(Employee employee){
//根据雇员的id得到该雇员应当添加到哪条链表
int employeelinkedListNo = hashFun(employee.id);
//将employee 添加到对应的链表中
employeelinkedListArray[employeelinkedListNo].add(employee);
}
//遍历所有的链表,遍历hashtable
public void list(){
for (int i = 0; i < size; i++) {
employeelinkedListArray[i].list(i);
}
}
//根据输入的id,查找雇员
public void findEmployeeById(int id){
//使用散列函数确定到哪条链表查找
int employeelinkedListNo = hashFun(id);
Employee employee = employeelinkedListArray[employeelinkedListNo].findEmployeeById(id);
if(employee!=null){ //找到
System.out.printf("在第%d条链表中找到雇员 id=%dn",(employeelinkedListNo+1),id);
}else{
System.out.println("在哈希表中,没有找到该雇员");
}
}
//根据id删除雇员
public void deleteEmployee(int id){
int employeelinkedListNo = hashFun(id);
employeelinkedListArray[employeelinkedListNo].deleteEmployee(id);
}
//编写散列函数,使用一个简单的取模法
public int hashFun(int id){
return id % size;
}
}
//定义一个表示雇员的结点类
class Employee{
public int id;
public String name;
public Employee next;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
}
//创建EmployeelinkedList,表示链表,用于实现对链表操作的方法
class EmployeelinkedList{
//头指针,因此这个链表的head是直接指向第一个Employee
private Employee head;
//添加雇员到链表
public void add(Employee employee){
//如果是添加第一个雇员
if(head==null){
head = employee;
return;
}
//如果不是第一个雇员,则使用一个辅助的指针,辅助定位到最后
Employee curEmployee = head;
while(curEmployee.next!=null){
curEmployee = curEmployee.next;
}
//退出时直接将employee加入链表
curEmployee.next = employee;
}
//遍历链表的雇员信息
public void list(int no){
if(head==null){//说明链表为空
System.out.println("第"+(no+1)+"链表为空");
return;
}
System.out.print("第"+(no+1)+"链表的信息为");
Employee curEmployee = head;
while(true){
System.out.printf("=> id=%d name=%s t",curEmployee.id,curEmployee.name);
if(curEmployee.next==null){
break;
}
curEmployee = curEmployee.next;
}
System.out.println();
}
//根据id查找雇员
//如果查找到,就返回Employee,如果没有找到,就返回null
public Employee findEmployeeById(int id){
if(head==null){
System.out.println("链表为空");
return null;
}
Employee curEmployee = head;
while(true){
if(curEmployee.id == id){ //找到
break;//这时curEmployee就指向要查找的雇员
}
if(curEmployee.next==null){ //说明遍历当前链表没有找到该雇员
curEmployee = null;
break;
}
curEmployee = curEmployee.next;
}
return curEmployee;
}
//根据id删除雇员
//其他方式:新建一个头头结点指向head
public void deleteEmployee(int id){
if(head==null){
System.out.println("链表为空");
return;
}
//仅有一个结点时,删除链表的第一个结点
if(head.next == null){
head = null;
return;
}
//有多个结点时,删除第一个结点
if(head.id == id){
head = head.next;
return;
}
Employee curEmployee = head;
while(true){
if(curEmployee.next == null){
System.out.println("没有找到");
break;
}
if(curEmployee.next.id == id){
curEmployee.next = curEmployee.next.next;
break;
}
curEmployee = curEmployee.next;
}
}
}



