11.哈希表
代码示例:
有三部分:
1.员工实体Emp,里面有个重要的next属性,标注下一个员工
2.员工链表EmpLinkedList,里面包含了head头部信息,以及链表的增删改查
3.链表数组MyHashTable,暴露给上层调用的,里面会根据id定位到具体操作的链表,再进行基本操作
/**
* 模拟hashTable
*/
public class HashTableDemo {
public static void main(String[] args) {
MyHashTable myHashTable = new MyHashTable(5);
Emp emp1 = new Emp(1, "吴孟达", 18);
Emp emp2 = new Emp(2, "刘丹", 19);
Emp emp3 = new Emp(3, "吴振", 20);
Emp emp6 = new Emp(6, "吴东霞", 20);
myHashTable.add(emp1);
myHashTable.add(emp2);
myHashTable.add(emp3);
myHashTable.add(emp6);
myHashTable.list();
}
}
class MyHashTable {
private EmpLinkedList[] empLinkedLists;
//数组大小
private int size;
public MyHashTable(int size) {
this.size = size;
empLinkedLists = new EmpLinkedList[size];
}
/**
* 添加元素
*
* @param emp 添加的元素
*/
public void add(Emp emp) {
int index = emp.getId() % size;
//获取对应下标的EmpLinkedList
if (empLinkedLists[index] == null) {
empLinkedLists[index]=new EmpLinkedList();
}
empLinkedLists[index].add(emp);
}
/**
* 遍历
*/
public void list() {
for (int i = 0; i < empLinkedLists.length; i++) {
if (empLinkedLists[i] != null) {
System.out.println("数组:" + i);
empLinkedLists[i].list();
}
}
}
}
/**
* 员工管理的链表
*/
class EmpLinkedList {
private Emp head;
/**
* 添加元素
*
* @param emp 需要添加的元素
*/
public void add(Emp emp) {
if (head == null) {
head = emp;
return;
}
Emp curret = head;
//查找链表的最后一个节点
while (curret.getNext() != null) ;
curret.setNext(emp);
}
/**
* 遍历该链表
*/
public void list() {
if (head == null) {
System.out.println("该链表为空!");
return;
}
Emp curent = head;
while (curent != null) {
System.out.println(curent);
curent = curent.getNext();
}
}
}
/**
* 员工实体类
*/
class Emp {
//员工id
private int id;
//员工名称
private String name;
//员工姓名
private int age;
//下一个节点
private Emp next;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Emp(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Emp getNext() {
return next;
}
public void setNext(Emp next) {
this.next = next;
}
@Override
public String toString() {
return "Emp{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
输出:可以清晰看见链表数组的元素分布
数组:0
数组:1
Emp{id='1', name='吴孟达', age=18}
Emp{id='6', name='吴东霞', age=20}
数组:2
Emp{id='2', name='刘丹', age=19}
数组:3
Emp{id='3', name='吴振', age=20}
数组:4
查找
1.链表类
class EmpLinkedList {
private Emp head;
...
/**
* 根据id查找emp
*
* @param id 查找的id
* @return
*/
public Emp getEmpById(int id) {
if (head == null) {
return null;
}
Emp curent = head;
while (true) {
if (curent.getId() == id) {
break;
}
//找到最后一个还不是,将current置为null返回
if (curent.getNext() == null) {
curent = null;
break;
}
curent = curent.getNext();
}
return curent;
}
}
2.链表数组类:
class MyHashTable {
private EmpLinkedList[] empLinkedLists;
//数组大小
private int size;
...
public Emp getEmpById(int id) {
int index = id % size;
if (empLinkedLists[index] == null) {
return null;
}
return empLinkedLists[index].getEmpById(id);
}
...
}
3.测试输出:
public static void main(String[] args) {
MyHashTable myHashTable = new MyHashTable(5);
Emp emp1 = new Emp(1, "吴孟达", 18);
Emp emp2 = new Emp(2, "刘丹", 19);
Emp emp3 = new Emp(3, "吴振", 20);
Emp emp6 = new Emp(6, "吴东霞", 20);
myHashTable.add(emp1);
myHashTable.add(emp2);
myHashTable.add(emp3);
myHashTable.add(emp6);
myHashTable.list();
System.out.println("------------------查找--------");
Emp emp = myHashTable.getEmpById(6);
if (emp == null) {
System.out.println("没找到!");
} else {
System.out.println(emp.toString());
}
}
查找部分输出:
------------------查找--------
Emp{id='6', name='吴东霞', age=20}
找一个没有的:Emp emp = myHashTable.getEmpById(100);
------------------查找--------
没找到!
更新和删除也是一样的套路!