哈希表是通过数组来管理链表,实现了对大量数据的简易管理,不仅提高了效率,又能优化查找。
思路分析: 1.创建一个链表,用于存储雇员数据,同时需要创建一个雇员类,其中有id以及可识别的next 2.创建MyHashTable类,在其中创建一个数组,用于存储链表 3.根据id值存入到MyHashTable的链表数组中去 4.存入方法:使用散列函数 编写一个方法,首先获得链表数组的长度size,长度由构造器创建 该方法的返回值取决于当前id值%上长度,即id%size就是所需的值
一、创建存储类节点
传统的节点创建方式,不过需要注意,此次哈希表需要通过id值进行数据的访问,所以必要的属性有id和next,其余的为需存储的数据属性
public class Emp {
private int id;
private String name;
public Emp next;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "name = " + this.name +",id = " + id;
}
}
二、创建单向链表
与之前创建单向链表的方式相同,同时完善其中所需要的各种方法
public class EmpLinkedList {
//默认头节点
Emp head;
//遍历链表
public void list(){
if(isEmpty()){
System.out.println("链表为空");
return;
}
Emp temp = head;
while (true){
System.out.print(temp + "\t");
if(temp.next == null){
break;
}
temp = temp.next;
}
}
//创建添加数据的方法
public void add(Emp emp){
if(head == null){
head = emp;
return;
}
Emp temp = head;//创建一个临时变量
while (true) {
if(temp.getId() == emp.getId()){
System.out.println("当前数据已存在");
return;
}
if (temp.next == null) {
temp.next = emp;
break;
}
temp = temp.next;
}
}
//创建删除方法
public void delete(int id){
if(isEmpty()){
System.out.println("链表为空");
return;
}
if(head.getId() == id){
if(head.next == null){
head = null;
}else{
head = head.next;
}
return;
}
Emp temp = head;
while (true){
if(temp.next == null){
System.out.println("未找到该数据");
return;
}
if(temp.next.getId() == id){
temp.next = temp.next.next;
break;
}
temp = temp.next;
}
}
//修改id对应数据
public void update(Emp emp){
if(isEmpty()){
System.out.println("链表为空");
return;
}
Emp temp = head;
while (true){
if(temp.getId() == emp.getId()){
temp.setName(emp.getName());
break;
}
if(temp.next == null){
System.out.println("未找到待修改值");
return;
}
temp = temp.next;
}
}
//根据id查找元素
public Emp search(int id){
if(isEmpty()){
System.out.println("链表为空");
return null;
}
Emp temp = head;
while (true){
if(temp.getId() == id){
break;
}
if(temp.next == null){
System.out.println("未找到该元素");
return null;
}
temp = temp.next;
}
return temp;
}
//判断当前链表是否为空
public boolean isEmpty(){
return head == null;
}
//获取当前链表长度
public int getSize(){
if(isEmpty()){
return 0;
}
int i = 1;
Emp temp = head;
while (true){
if(temp.next == null){
break;
}
i++;
temp = temp.next;
}
return i;
}
}
三,创建哈希表并完善方法
public class MyHashTable {
private EmpLinkedList[] empLinkedLists;
private int size;
//初始化链表数组以及链表长度
public MyHashTable(int size) {
this.empLinkedLists = new EmpLinkedList[size];
this.size = size;
for(int i = 0;i < size;i++){//如果不初始化创建对象,那么进行链表操作的时候会报空指针异常
empLinkedLists[i] = new EmpLinkedList();
}
}
//创建散列函数获取存储方法
public int getLocation(int id){
return id%size;
}
//创建添加方法
public void add(Emp emp){
int empNo = getLocation(emp.getId());//获取当前存储在链表数组的位置
empLinkedLists[empNo].add(emp);//存进去
}
//创建遍历方法
public void list(){
for (int i = 0;i < size;i++){
empLinkedLists[i].list();
System.out.println();
}
}
//创建删除方法
public void delete(int id){
int location = getLocation(id);
empLinkedLists[location].delete(id);
}
//创建修改方法
public void update(Emp emp){
empLinkedLists[getLocation(emp.getId())].update(emp);
}
//创建查找方法
public Emp search(int id){
return empLinkedLists[getLocation(id)].search(id);
}
}