哈希表基本介绍
散列表(Hash table,也叫哈希表),是根据关键码值(Keyvalue)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
代码实现
首先编写一个存放节点的类
package com.athome.hashtab;
/**
* Description:
* Author:江洋大盗
* Date:2021/1/21 16:12
*/
public class Hero {
int id;//武将编号
String name;//名字
Hero next;
public Hero(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "武将{" +
"姓名:" + name +
", 编号:" + id +
'}';
}
}
编写链表:
package com.athome.hashtab;
/**
* Description:
* Author:江洋大盗
* Date:2021/1/21 16:10
*/
public class LinkedListHero {
private Hero head = null;
/**
* 直接添加到链表最后
*
* @param hero 待添加的节点
*/
public void add(Hero hero) {
if (head == null) {
head = hero;
return;
}
Hero temp = head.next;
while (temp != null) {
temp = temp.next;
}
temp = hero;
}
/**
* 按照武力值插入
*
* @param hero 待插入节点
*/
public void addById(Hero hero) {
if (head == null) {
head = hero;
return;
}
if (head.id > hero.id) {
hero.next = head;
head.next = hero;
return;
}
Hero temp = head;
while (temp.next != null) {
if (temp.next.id > hero.id) {
break;
}
temp = temp.next;
}
hero.next = temp.next;
temp.next = hero;
}
/**
* 删除节点
*
* @param id 待删除的编号
*/
public void delHero(int id) {
if (head == null) {
return;
}
if (head.id == id) {
head = head.next;
return;
}
Hero temp = head;
while (temp.next != null) {
if (temp.id == id) {
temp.next = temp.next.next;
return;
}
temp = temp.next;
}
System.out.println("没有找到要删除的武将");
}
/**
* @param id 待查找的武将编号
* @return 返回查询结果,若没有找到返回null
*/
public Hero find(int id) {
if (head == null) {
return null;
}
Hero temp = head;
while (temp != null) {
if (temp.id == id) {
return temp;
}
temp = temp.next;
}
return null;
}
/**
* 打印链表
*/
public void list() {
if (head == null) {
return;
}
Hero temp = head;
while (temp != null) {
System.out.print("-->" + temp);
temp = temp.next;
}
System.out.println();
}
/**
* @return 如果链表为空,返回true,否则返回false
*/
public boolean isEmpty() {
return head == null;
}
}
编写哈希表:
package com.athome.hashtab;
/**
* Description:
* Author:江洋大盗
* Date:2021/1/21 17:04
*/
public class HashTable {
private final int size;
LinkedListHero[] listHeroes;
public HashTable(int size) {
this.size = size;
listHeroes = new LinkedListHero[size];
//初始化链表数组
for (int i = 0; i < size; i++) {
listHeroes[i] = new LinkedListHero();
}
}
/**
* @param power 武将武力值
* @return 返回其所在的链表数组下标
*/
private int HashCode(int power) {
return power % size;
}
/**
* 添加方法
*
* @param hero 待添加的英雄
*/
public void add(Hero hero) {
int hashCode = HashCode(hero.id);
listHeroes[hashCode].addById(hero);
}
/**
* @param id 待查找的武将编号
*/
public void find(int id) {
int hashCode = HashCode(id);
Hero hero = listHeroes[hashCode].find(id);
if (hero == null) {
System.out.println("没有找到该武将");
} else {
System.out.println("您查找的武将为:" + hero);
}
}
/**
* 删除武将
*
* @param id 待删除的武将编号
*/
public void delete(int id) {
int hashCode = HashCode(id);
listHeroes[hashCode].delHero(id);
}
/**
* 打印链表
*/
public void list() {
for (int i = 0; i < size; i++) {
if (listHeroes[i].isEmpty()) {
System.out.println("第" + (i + 1) + "条链表为空。");
} else {
System.out.print("第" + (i + 1) + "条链表:");
listHeroes[i].list();
}
}
}
}
主方法测试:
package com.athome.hashtab;
import java.util.Scanner;
/**
* Description:
* Author:江洋大盗
* Date:2021/1/21 17:25
*/
public class Demo {
public static void main(String[] args) {
String choose;
HashTable hashTab = new HashTable(7);
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("==========================");
System.out.println("add:添加元素");
System.out.println("list:打印元素");
System.out.println("find:查找元素");
System.out.println("delete:删除元素");
System.out.println("exit:退出");
System.out.print("请输入您的选择:");
choose = scanner.next();
switch (choose) {
case "add":
System.out.print("请输入武将id:");
int id = scanner.nextInt();
System.out.print("请输入武将姓名:");
String name = scanner.next();
hashTab.add(new Hero(id, name));
break;
case "list":
hashTab.list();
break;
case "find":
System.out.print("请输入要查询的武将id:");
id = scanner.nextInt();
hashTab.find(id);
break;
case "delete":
System.out.print("请输入要删除的武将id:");
id = scanner.nextInt();
hashTab.delete(id);
break;
case "exit":
loop = false;
break;
}
}
}
}
结语
只要能收获甜蜜,荆棘丛中也有蜜蜂忙碌的身影,未来的你一定会感谢现在努力的自己。