1,哈希表基本介绍
- 散列表(HashTable,也叫哈希表),是根据关键码值(Key Value)而进行访问的数据结构。也就是说,通过关键码值映射到表中的一个位置来访问记录,以加快查找的速度。这个映射函数叫散列函数,存放记录的数组叫做散列表
- 基本数据结构为 数组 + 链表;通过键值获取到数组索引位置,存储到数组中,数组中该索引位置如果已经存在数据,则在该索引位置上构造链表。
2,哈希表示意图
3,哈希表代码实现
package com.self.datastructure.hash;
import lombok.Data;
import lombok.ToString;
import java.util.Scanner;
import java.util.UUID;
public class MyHashTable {
public static void main(String[] args) {
SelfHash selfHash = new SelfHash();
for (int i = 0; i < 100; i++) {
selfHash.put(i + "", new Employee(i + "name", i + ""));
}
System.out.println("总数: " + selfHash.size());
for (;;) {
System.out.println("输入要删除的元素编号");
Scanner scanner = new Scanner(System.in);
String inputId = scanner.nextLine();
System.out.println(selfHash.get(inputId));
System.out.println(selfHash.remove(inputId));
System.out.println(selfHash.get(inputId));
}
}
static class SelfHash {
private static int DEFAULT_SIZE = 16;
private static int length = DEFAULT_SIZE;
private static int size;
private Node[] nodeArray;
public SelfHash() {
this(length);
}
public SelfHash(int size) {
this.length = size;
nodeArray = new Node[this.length];
}
public void put(String key, Employee value) {
if (nodeArray == null) nodeArray = new Node[DEFAULT_SIZE];
int targetIndex = key.hashCode() % length;
if (null == nodeArray[targetIndex]) {
nodeArray[targetIndex] = new Node(key, value);
size++;
} else {
Node node = nodeArray[targetIndex];
Node preNode = node;
for (;node != null;) {
if (node.getKey().equals(key)) {
node.setValue(value);
return;
}
preNode = node;
node = node.getNextNode();
}
preNode.setNextNode(new Node(key, value));
size++;
}
}
public Employee get(String key) {
int targetIndex = key.hashCode() % length;
Node node = nodeArray[targetIndex];
for (;null != node;) {
if (key.equals(node.getKey())) {
return node.getValue();
}
node = node.getNextNode();
}
return null;
}
public boolean remove(String key) {
int targetIndex = key.hashCode() % length;
Node node = nodeArray[targetIndex];
Node preNode = node;
for (;null != node;) {
if (key.equals(node.getKey())) {
if (preNode == node) {
nodeArray[targetIndex] = node.getNextNode();
} else {
preNode.setNextNode(node.getNextNode());
}
return true;
}
preNode = node;
node = node.getNextNode();
}
return false;
}
public void showArray() {
if (size == 0) {
System.out.println("数据为空...");
return;
}
for (int i = 0; i < length; i++) {
Node node = nodeArray[i];
for (;null != node;) {
System.out.println("Node: INDEX: " + i + ", " + node.getValue());
node = node.getNextNode();
}
}
}
public int size() {
return size;
}
public int length() {
return length;
}
}
@Data
static class Node {
private String key;
private Employee value;
private Node nextNode;
public Node() {}
public Node(String key, Employee value) {
this(key, value, null);
}
public Node(String key, Employee value, Node nextNode) {
this.key = key;
this.value = value;
this.nextNode = nextNode;
}
}
@Data
@ToString
static class Employee {
String id;
String name;
public Employee() {}
public Employee(String name) {
this(UUID.randomUUID().toString().replaceAll("-", ""), name);
}
public Employee(String id, String name) {
this.id = id;
this.name = name;
}
}
}