1. 基本介绍
- 散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
![在这里插入图片描述](https://img-blog.csdnimg.cn/4801e995d36e4eb6b86d367c0e148130.png)
2. 应用案例
- 当公司有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址…),当输入该员工的id时,要求能查找到该员工的所有信息.
- 要求:
- 不使用数据库,速度越快越好=>哈希表(散列)
- 添加时,保证按照id从低到高插入 [如果id不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?]
- 使用链表来实现哈希表,该链表不带表头[即:链表的第一个结点就存放员员信息]
![在这里插入图片描述](https://img-blog.csdnimg.cn/a132dbc5dc9847cbaaa7eaa6635216a5.png)
3. 代码实现
public class HashTabDemo {
public static void main(String[] args) {
HashTab hashTab = new HashTab(7);
People p1 = new People(1, "老大");
People p2 = new People(2, "老二");
People p3 = new People(3, "老三");
People p123 = new People(123, "一二三");
People p456 = new People(456, "四五六");
People p789 = new People(789, "七八九");
hashTab.add(p1);
hashTab.add(p2);
hashTab.add(p3);
hashTab.add(p123);
hashTab.add(p456);
hashTab.add(p789);
hashTab.list();
hashTab.findPeopleById(123);
hashTab.findPeopleById(996);
}
}
class People {
public int id;
public String name;
public People next;
public People(int id, String name) {
this.id = id;
this.name = name;
}
}
class PeopleLinkedList {
private People head;
public void add(People people) {
if (head == null) {
head = people;
return;
}
People curPeople = head;
while (true) {
if (curPeople.next == null) {
break;
}
curPeople = curPeople.next;
}
curPeople.next = people;
}
public void list(int no) {
if (head == null) {
System.out.println("第 " + (no + 1) + " 链表为空");
return;
}
System.out.print("第 " + (no + 1) + " 链表数据为");
People curPeople = head;
while (true) {
System.out.printf(" id=%d name=%s\t", curPeople.id, curPeople.name);
if (curPeople.next == null) {
break;
}
curPeople = curPeople.next;
}
System.out.println();
}
public People findPeopleById(int id) {
if (head == null) {
System.out.println("链表为空");
return null;
}
People curPeople = head;
while (true) {
if (curPeople.id == id) {
break;
}
if (curPeople.next == null) {
curPeople = null;
break;
}
curPeople = curPeople.next;
}
return curPeople;
}
}
class HashTab {
private PeopleLinkedList[] peopleLinkedListArray;
private int size;
public HashTab(int size) {
this.size = size;
this.peopleLinkedListArray = new PeopleLinkedList[size];
for (int i = 0; i < size; i++) {
peopleLinkedListArray[i] = new PeopleLinkedList();
}
}
public int hashFun(int id) {
return id % size;
}
public void add(People people) {
int i = hashFun(people.id);
peopleLinkedListArray[i].add(people);
}
public void list() {
for (int i = 0; i < size; i++) {
peopleLinkedListArray[i].list(i);
}
}
public void findPeopleById(int id) {
int i = hashFun(id);
People peopleById = peopleLinkedListArray[i].findPeopleById(id);
if (peopleById != null) {
System.out.printf("在第%d条链表中找到 id = %d 的元素\n", i + 1, id);
} else {
System.out.printf("没有找到 id = %d 的元素\n", id);
}
}
}
第 1 链表为空
第 2 链表数据为 id=1 name=老大 id=456 name=四五六
第 3 链表数据为 id=2 name=老二
第 4 链表数据为 id=3 name=老三
第 5 链表数据为 id=123 name=一二三
第 6 链表数据为 id=789 name=七八九
第 7 链表为空
在第5条链表中找到 id = 123 的元素
没有找到 id = 996 的元素