哈希表(散列)
哈希表:不使用数据库;节省内存;速度快 — 存储、查找数据
根据键值码值直接进行访问的数据结构,通过把关键码值映射到表中一个位置来访问记录,以加快查找到速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表
查找数据:
java 程序 — 缓存层(1. 缓存产品 redis、memcache;2.自己写:哈希表:数组+链表/数组+二叉树)— 数据库
将常用的数据从数据库加载到哈希表中,可以有多级缓存,提升查找效率
问题
要求:当新员工来报道,将其信息(id、年龄、性别、名字)加入,当输入该员工id时,能够查找到其所有信息
保证id从低到高添加
class Emp{id;name;address;} // 管理节点的信息
class EmpLinkedList{ // 管理链表的节点
Emp head = null;
// 相关的对雇员的操作
// add
// list
// find
// del
// 散列函数 决定id对应哪个链表
}
class HashTable{ // 管理所有的链表
EmpLinkedList[]
empLikedListArr
// 调用对链表的操作 增删改查
}
package hash;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
import java.util.Scanner;
public class HashTest {
public static void main(String[] args) {
// create a hash table
HashTab hashTab = new HashTab(7);
// menu
String key = "";
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("add: add emp");
System.out.println("list: list emp");
System.out.println("exit: exit system");
System.out.println("find: find emp");
System.out.println("delete: delete emp");
key = scanner.next();
switch (key){
case "add":
System.out.println("input id");
int id = scanner.nextInt();
System.out.println("input name");
String name = scanner.next();
// create emp
Emp emp = new Emp(id, name);
hashTab.add(emp);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.println("input id you want find");
id = scanner.nextInt();
hashTab.find(id);
break;
case "delete":
System.out.println("input id you want to delete");
id = scanner.nextInt();
hashTab.delete(id);
break;
case "exit":
scanner.close();
System.exit(0);
break;
default:
break;
}
}
}
}
// hash table
class HashTab{
private EmpLinkedList[] empLinkedLists;
private int size; // number of linkedlists
// constructor
public HashTab(int size){
this.size = size;
empLinkedLists = new EmpLinkedList[size];
// initialize each linkedlist separately
for (int i = 0; i < size; i++) {
empLinkedLists[i] = new EmpLinkedList();
}
}
// add emp
public void add(Emp emp){
// get emp's position according to emp's id
int empLinkedListNo = hashFun(emp.id);
// add emp to relative linkedlist
empLinkedLists[empLinkedListNo].add(emp);
}
// traverse all linkedlists
public void list(){
for(int i = 0; i<size; i++){
empLinkedLists[i].traverse(i);
}
}
// find emp
public void find(int id){
// hash function
int empLinkedListNum = hashFun(id);
Emp emp = empLinkedLists[empLinkedListNum].findEmpById(id);
if(emp!=null){
System.out.printf("id %d is found it in %d\t",id,empLinkedListNum);
System.out.printf("id is %d, name is %s",id,emp.name);
}else{
System.out.println("not find it ");
}
}
public void delete(int id){
int empLinkedListNum = hashFun(id);
empLinkedLists[empLinkedListNum].delete(id);
}
// hash function, module
public int hashFun(int id){
return id % size;
}
}
class Emp{
public int id;
public String name;
public Emp next; // default is null
public Emp(int id, String name){
super();
this.id = id;
this.name = name;
}
}
class EmpLinkedList{
// head points to the first Emp, so this linkedlist head is meaningful
private Emp head; // default is null
// add Emp to linkedlist
// 1. assume add id from small to big
// add emp to the last position of current linkedlist
public void add(Emp emp){
// if it is the first emp
if(head == null){
head = emp;
return;
}
// if it is not the first emp, use assistant pointer
Emp curEmp = head;
while(true){
if(curEmp.next==null){
break;
}
curEmp = curEmp.next;
}
// add into linkedlist when out of the loop
curEmp.next = emp;
}
// traverse linkedlist
public void traverse(int num){
if(head == null){
System.out.println("list "+num+" is null");
return;
}
Emp temp = head;
while(true){
System.out.printf("list " +num+ " => id = %d name = %s\t",temp.id,temp.name);
if(temp.next==null){
break;
}
temp = temp.next;
}
System.out.println();
}
// find emp
public Emp findEmpById(int id){
if(head==null){
System.out.println("list is null");
return null;
}
Emp curEmp = head;
while(true){
if(curEmp.id == id){
break; // find it!
}
if(curEmp.next == null){
// not find it
curEmp = null;
break;
}
curEmp = curEmp.next;
}
return curEmp;
}
public void delete(int id){
if(head == null){
System.out.println("list is null");
return;
}
Emp curEmp = head;
if(curEmp.id == id){
curEmp = curEmp.next;
}
boolean flag = false;
while(true){
if(curEmp.next==null){
break;
}
if(curEmp.next.id==id){
flag = true;
break;
}
curEmp = curEmp.next;
}
if(flag){
curEmp.next = curEmp.next.next;
}else{
System.out.println("not fount it");
}
}
}