8、哈希表(散列)
-
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
-
例题:
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址…),当输入该员工的id 时,要求查找到该员工的所有信息。
要求:- 不使用数据库,速度越快越好=>哈希表(散列);
- 添加时,保证按照id 从低到高插入;
- 使用链表来实现哈希表, 该链表不带表头[即: 链表的第一个结点就存放雇员信息]。
-
图解:
//哈希表测试
public class HashTableTest {
public static void main(String[] args) {
HashTable hashTable = new HashTable(3);
hashTable.add(1, "张三");
hashTable.add(5, "电七");
hashTable.add(2, "李四");
hashTable.add(6, "钱八");
hashTable.add(9, "孙十");
hashTable.add(3, "王五");
hashTable.add(4, "赵六");
hashTable.show();
hashTable.findById(6);
hashTable.deleteById(3);
hashTable.deleteById(6);
hashTable.deleteById(9);
System.out.println("删除后:");
hashTable.show();
}
}
//哈希表
class HashTable {
private int size;
private EmployLinkList[] employLinkListArray;
//初始化
public HashTable(int size) {
this.size = size;
employLinkListArray = new EmployLinkList[size];
for (int i = 0; i < size; i++) {
employLinkListArray[i] = new EmployLinkList();
}
}
//添加
public void add(int id, String name) {
Employ employ = new Employ(id, name);
int index = hashFun(id);
employLinkListArray[index].add(employ);
}
//遍历
public void show() {
for (int i = 0; i < size; i++) {
System.out.println("第" + (i + 1) + "条链表:");
employLinkListArray[i].show();
}
}
//查找
public Employ findById(int id) {
int index = hashFun(id);
Employ temp = employLinkListArray[index].findById(id);
if (temp == null) {
System.out.println("查找结果为空");
} else {
System.out.println("查找结果:" + temp.toString());
}
return temp;
}
//删除
public void deleteById(int id){
int index = hashFun(id);
employLinkListArray[index].deleteById(id);
}
//散列函数——简单取模法
public int hashFun(int id) {
return id % size;
}
}
//员工链表
class EmployLinkList {
private Employ head = null;//不使用单独的表头结点
//添加员工结点(按顺序插入)
public void add(Employ employ) {
if (head == null) {
head = employ;
} else {
Employ temp = head;
//如果插入结点id小于头结点id,将插入结点指向原本头节点,并让插入节点成为新的头结点
if (temp.id > employ.id) {
employ.next = temp;
head = employ;
} else {
//遍历,如果遍历至末尾结点仍id仍小于插入结点,则插入到末尾
//否则,插入到链表中间
while (temp.next != null && temp.next.id < employ.id) {
temp = temp.next;
}
if (temp.next == null) {
temp.next = employ;
} else {
employ.next = temp.next;
temp.next = employ;
}
}
}
}
//遍历员工
public void show() {
Employ temp = head;
if (temp == null) {
System.out.println("=> 链表为空");
}
while (temp != null) {
System.out.println("=> " + temp.toString());
temp = temp.next;
}
}
//查找员工
public Employ findById(int id) {
Employ temp = head;
while (temp != null) {
if (temp.id == id) {
break;
}
temp = temp.next;
}
return temp;
}
//删除员工
public void deleteById(int id){
Employ temp = head;
if(head.id==id){
head=head.next;
}else{
while (temp.next!=null&&temp.next.id!=id){
temp=temp.next;
}
if(temp.next==null){
System.out.println("该结点不存在,无法删除");
}else{
temp.next=temp.next.next;
}
}
}
}
//员工结点类
class Employ {
int id;
String name;
String adress;
Employ next;
public Employ(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Employ{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}